為什么監聽不到開機廣播action.BOOT_COMPLETED
1. 說明
Android手機開機后,會發送android.intent.action.BOOT_COMPLETED廣播,監聽這個廣播就能監聽開機。
2. 代碼
注冊廣播
<receiver android:name="com.javen.broadcast.BootBroadCastReceiver" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" > </action> </intent-filter> </receiver>
添加權限
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
實現Receiver
public class BootRroadCastReceiver extends BroadcastReceiver { private final String ACTION_BOOT = "android.intent.action.BOOT_COMPLETED"; @Override public void onReceive(Context context, Intent intent) { if (ACTION_BOOT.equals(intent.getAction())) Toast.makeText(context, R.string.bootup_receiver, Toast.LENGTH_SHORT).show(); } }
3. 問題
Android API Leve 18(Android4.2)以上的時候,程序可以安裝在SD卡上。如果程序安裝在SD卡上,那么在BOOT_COMPLETED廣播發送之后,SD卡才會掛載,因此程序無法監聽到該廣播。
4. 解決方案
監聽SD卡的掛載。
5. 同時監聽的Intent-Filter問題以及SD卡監聽不到的問題
如果BOOT_COMPLETED和MEDIA_MOUNTED,MEDIA_EJECT寫在同一個intent-filter中,那么無法檢測到BOOT_COMPLETED,對於沒有SD卡的手機,只能檢測BOOT_COMPLETED,這樣就會導致無法檢測到開機了。
<receiver android:name="com.javen.receiver.SystemEventReceiver" android:permission="android.permission.RECEIVE_BOOT_COMPLETED" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> <intent-filter> <!-- SD卡已經成功掛載 --> <action android:name="android.intent.action.MEDIA_MOUNTED" /> <!-- sd卡存在,但還沒有掛載 --> <action android:name="android.intent.action.MEDIA_UNMOUNTED" /> <action android:name="android.intent.action.MEDIA_EJECT" /> <data android:scheme="file" /> </intent-filter> </receiver>
路過對你有幫助歡迎轉發 推薦 關注 轉發時請添加地址 http://www.cnblogs.com/zyw-205520/p/4773810.html