我有一個接收器類監聽幾個操作,但它不能捕獲android.intent.action.BOOT_COMPLETED
操作。我做錯了什麼?這裏是我的清單文件:BOOT_COMPLETED意圖操作的廣播無法正常工作
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<!--<receiver android:name=".OtherReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>-->
<receiver android:name="com.myApp.AppReceiver" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="com.myApp.wifitimer"/>
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" android:path="com.myApp" />
</intent-filter>
</receiver>
,因爲它可以看到我再次添加許可的接收器內,接收器的名稱獲取類的全名,因爲這answer建議。
這裏是廣播接收器類:
@Override
public void onReceive(Context arg0, Intent arg1) {
String action1 = arg1.getAction();
if(action1.equals(Intent.ACTION_BOOT_COMPLETED)) {
Log.d("receiver","action is: boot");
}
if(action1.equals("android.intent.action.PACKAGE_REPLACED")) {
Log.d("receiver","action is: package");
}
}
當我運行的應用程序的接收器捕獲android.intent.action.PACKAGE_REPLACED
但是當我重新啓動手機接收器沒有趕上BOOT_COMPLETED
。
但是,當我在.OtherReceiver
Mainfest文件中註釋它可以捕獲它!
這裏是這個類的代碼:
public class OtherReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
String action = arg1.getAction();
if(action.equals(Intent.ACTION_BOOT_COMPLETED)) {
Log.d("new receiver","action is: boot");
}
}
}
只是一樣的另一種。所以我的問題是爲什麼我需要爲BOOT_COMPLETED
操作定義一個單獨的接收器?
編輯:我也試圖通過ADB發送動作根據this,且無任何許可,我可以用AppReceiver類抓住它:從你的<receiver>
am broadcast -a android.intent.action.BOOT_COMPLETED -c android.intent.category.HOME -n com.blubuk/.AppReciever