如何在 Android 上声明一个显式接收者 9
How to declare an explicit receiver on Android 9
我在清单中声明接收器并让它工作时遇到了一些麻烦。
我知道自 android 8.0 以来,清单中不能声明任何隐式广播接收器,但它可以是显式声明的接收器。
所以我在清单中这样声明我的:
<receiver android:name=".util.AppReceiver" android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="com.ibermatica.mime.starttracking" />
</intent-filter>
</receiver>
之后,我在 phone 中以调试模式安装应用程序,并在 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
但是没有任何反应,也没有调用接收器。出了什么问题或我必须做些什么来解决这个问题?
提前致谢!
I know since android 8.0 there can't be any implicit broadcast receivers declared in manifest, but it can be explicit receivers declared.
"Explicit" 和 "implicit" 是用于指代 Intent
对象类型的术语,而不是清单条目。
But nothing happens, nor the receiver gets called.
正确。您正在命令行上创建隐式 Intent
,并且隐式 Intent
广播通常不适用于 Android 8.0+。
尝试:
adb shell am broadcast -n com.whatever/.util.AppReceiver -a com.ibermatica.mime.starttracking
将 com.whatever
替换为您的应用程序 ID。
我在清单中声明接收器并让它工作时遇到了一些麻烦。
我知道自 android 8.0 以来,清单中不能声明任何隐式广播接收器,但它可以是显式声明的接收器。
所以我在清单中这样声明我的:
<receiver android:name=".util.AppReceiver" android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="com.ibermatica.mime.starttracking" />
</intent-filter>
</receiver>
之后,我在 phone 中以调试模式安装应用程序,并在 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
但是没有任何反应,也没有调用接收器。出了什么问题或我必须做些什么来解决这个问题?
提前致谢!
I know since android 8.0 there can't be any implicit broadcast receivers declared in manifest, but it can be explicit receivers declared.
"Explicit" 和 "implicit" 是用于指代 Intent
对象类型的术语,而不是清单条目。
But nothing happens, nor the receiver gets called.
正确。您正在命令行上创建隐式 Intent
,并且隐式 Intent
广播通常不适用于 Android 8.0+。
尝试:
adb shell am broadcast -n com.whatever/.util.AppReceiver -a com.ibermatica.mime.starttracking
将 com.whatever
替换为您的应用程序 ID。