我有一个接收器类监听几个动作,但它无法捕捉到android.intent.action.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>
可以看出,我在接收器中再次添加了权限,并且接收器的名称得到了这个答案所暗示的类的全名。
这是广播接收器类:
@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
.
但是,当我在 Mainfest 文件中发表评论时,.OtherReceiver
它可以捕捉到它!
这是这个类的代码:
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");
}
}
}
just the same as the other one. So my question is why I need define a separate receiver for the BOOT_COMPLETED
action?
Edit: I also tried to send the action via adb according to this, and without any permission I could catch it with the AppReceiver class:
am broadcast -a android.intent.action.BOOT_COMPLETED -c android.intent.category.HOME -n com.blubuk/.AppReciever