我有一个接收器类监听几个 Action ,但它无法捕获 android.intent.action.BOOT_COMPLETED
Action 。我做错了什么?这是我的 list 文件:
<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
。
但是,当我在 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");
}
}
}
和另一个一样。所以我的问题是为什么我需要为 BOOT_COMPLETED
操作定义一个单独的接收器?
编辑:我还尝试根据 this 通过 adb 发送操作,并且在没有任何许可的情况下,我可以使用 AppReceiver 类捕获它:
am broadcast -a android.intent.action.BOOT_COMPLETED -c android.intent.category.HOME -n com.blubuk/.AppReciever
最佳答案
首先,删除android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
来自你的 <receiver>
元素。
其次,你的<data>
你的一部分 <intent-filter>
正在应用于所有<action>
<intent-filter>
中的元素 ,你不想要的。没有 Uri
在 ACTION_BOOT_COMPLETED
.
但是,与其创建单独的 <receiver>
元素,你可以创建一个单独的 <intent-filter>
原件上的元素 <receiver>
元素。移动你的<action android:name="android.intent.action.BOOT_COMPLETED" />
到新<intent-filter>
(也许那个 com.myApp.wifitimer
也是一个),这样它们就不会受到 <data>
的影响。你的第一个<intent-filter>
.
关于android - BOOT_COMPLETED Intent 操作的广播无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15723750/