我在清单中声明接收器并让它工作时遇到了一些麻烦。
我知道自从android 8.0以来,不能在manifest中声明任何隐式广播接收器,但它可以是声明的显式接收器。
所以我在清单中宣布我这样:
<receiver android:name=".util.AppReceiver" android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="com.ibermatica.mime.starttracking" />
</intent-filter>
</receiver>
之后,我在调试模式下在手机中安装应用程序并在onReceive方法中放置一个断点,该方法具有以下代码:
if(intent.getAction() != null){
switch (intent.getAction()){
case Util.START_TRACKING:
Intent i;
i = new Intent(context, LocationUpdatesService.class);
context.startService(i);
break;
default:
}
}
所以我使用此命令从后台使用应用程序的命令行发送广播消息:
adb shell am broadcast -a com.ibermatica.mime.starttracking
但没有任何事情发生,接收器也没有被调用。有什么问题或我该怎么做才能解决这个问题?
先感谢您!
我知道自从android 8.0以来,不能在manifest中声明任何隐式广播接收器,但它可以是声明的显式接收器。
“显式”和“隐式”是用于指代Intent
对象类型的术语,而不是清单条目。
但没有任何事情发生,接收器也没有被调用。
正确。您正在命令行上创建隐式Intent
,隐式Intent
广播通常不适用于Android 8.0+。
尝试:
adb shell am broadcast -n com.whatever/.util.AppReceiver -a com.ibermatica.mime.starttracking
在哪里用您的应用程序ID替换com.whatever
。