android-intent - 无法使用 FLAG_INCLUDE_STOPPED_PACKAGES 标志触发处于停止状态的 BroadcastReceiver
问题描述
用例:
单击按钮时启动操作背景。> 操作驻留在
BroadcastReceiver
中,Application1
并且 >Intent 正在从 中广播Application2
。
应用2 BroadcastReceiver
:
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Receiver triggered", Toast.LENGTH_SHORT).show();
}
}
应用2 AndroidManifest.xml
:
<receiver
android:name="com.my.package.MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="my.custom.action" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Application1 sendMyBroadcast()
(点击按钮调用):
public void sendMyBroadcast(){
Toast.makeText(getActivity(), "Sending broadcast", Toast.LENGTH_SHORT).show();
Intent intent = new Intent("my.custom.action");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
this.getActivity().sendBroadcast(intent);
}
我还尝试BroadcastReceiver
使用以下命令从 adb 测试我的:
adb shell am broadcast -a my.custom.action --include-stopped-packages
即使是上面的命令也无法触发BroadcastReceiver。但是如果我启动应用程序一次然后关闭它,那么上面的命令就能够触发广播接收器。
ps:正如您从代码中看到的那样,我正在从 a 广播意图Fragment
,尽管我认为这不会有任何区别。
编辑:也尝试使用明确的意图,但仍然没有工作
public void sendMyBroadcast(){
Toast.makeText(getActivity(), "Sending broadcast", Toast.LENGTH_SHORT).show();
Intent intent = new Intent("my.custom.action");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
ComponentName component = new ComponentName("com.my.package", "com.my.package.MyBroadcastReceiver");
intent.setComponent(component);
this.getActivity().sendBroadcast(intent);
}
解决方案
推荐阅读
- google-chrome - Chrome 和 Firefox 通过 Gitlab-runner 始终以无头模式运行
- android - Android:监控互联网连接的最佳方式
- curl - 415 不支持的媒体类型通过 CURL 将文件上传到 Box.com
- mysql - AWS Lambda 错误:Quit 时退出不活动超时
- angular - 以正确的顺序在管道中执行水龙头并忽略结果
- kubernetes - 使用`kubectl apply -f file.yaml`时传递环境变量
- javascript - 仅当对象位于两个数组中时才会选择 select 中的选项
- c - 如何随机显示数组中字符串的字符
- django - 如何修复在 django python 中找不到注销的反向
- python-2.7 - 如何解决 Ubuntu 18.04 中的“ImportError: No module named networkx”?