java - 如何在不同进程中使用 LocalBroadcastManager 在 Activity 和 Service 之间进行通信
问题描述
我有一个服务,它位于 Manifest 和MapboxMap
Activity 中定义的不同进程中,我只是想知道如何使用LocalBroadcastManager
.
我试图将服务上下文传递给LocalBroadcastManager.getInstance()
然后LocalBroadcast
在我的活动中注册一个。它注册成功,但无法从我的服务中获取信息!
这是我的代码,在我的服务中......
Intent locationIntent = new Intent("LocationIntent");
locationIntent.setAction("updatedLocations");
locationIntent.putExtra("list",updatedList);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(locationIntent);
...我在我的活动中注册它:
LocalBroadcastManager.getInstance(getApplicationContext()).registerReceive``r(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Timber.tag("localB").d("registered!");
if (intent != null && intent.getAction() != null &&
intent.getAction().equals("updatedLocations")) {
sawLocationsList = (HashMap<Integer,
MarkerItem>)intent.getSerializableExtra("list");
Timber.tag("sawL").d("updated");
}
}
} , new IntentFilter("LocationIntent"));
当我运行应用程序时,我的 Service 发送了广播,但我的 Activity 没有收到广播消息!
我认为这个问题是因为我的服务是在我的清单中的另一个进程中定义的......
android:name=".services.ForegroundService"
android:enabled="true"
android:exported="false"
android:process=":ForegroundService"
...但我想以这种方式交流,因为处于不同的流程有助于我实现电池效率目标。
解决方案
如何在不同进程中使用 LocalBroadcastManager 在 Activity 和 Service 之间进行通信
这是不可能的。中的“本地”LocalBroadcastManager
表示“此过程的本地”。LocalBroadcastManager
特别是在进程之间不起作用。
任何一个:
让活动和服务在同一个进程中,或者
使用某种形式的 IPC 在进程之间进行通信(系统广播
Messenger
等)
推荐阅读
- jquery - 在日历引导数组中推送数组
- firebase - 如何在flutter firebase中删除一对多关联
- c# - 创建一个可以返回控件列表的函数,其中包含控件标签中的特定信息
- swift - Swift 4:单击按钮到下一个视图控制器,但新页面的动画是“垂直覆盖”而不是“移动”到下一页
- css - Font Awesome:为什么我的箭头图标没有加载到 :after 实例上?
- javascript - JSDoc 嵌套函数不生成文档
- javascript - 击中空格后如何在 Sublime Text 3 中的括号之间自动插入空格
- python - /provider/'providers' 处的 Django NoReverseMatch 不是已注册的命名空间
- git - 将巨大的 SVN 存储库迁移到 GIT,并且 SVN 存储库上的冻结时间最短
- node.js - NodeJs - 接近堆限制分配失败 - JavaScript堆内存不足