Sin*_*ina 1 java android android-activity android-broadcast localbroadcastmanager
我有一个在 Manifest 和MapboxMapActivity中定义的不同进程中的服务,我只是想知道如何使用LocalBroadcastManager.
我试图将服务上下文传递给LocalBroadcastManager.getInstance()然后LocalBroadcast在我的活动中注册一个。它注册成功,但无法从我的服务中获取信息!
这是我的代码,在我的服务中...
Intent locationIntent = new Intent("LocationIntent");
locationIntent.setAction("updatedLocations");
locationIntent.putExtra("list",updatedList);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(locationIntent);
Run Code Online (Sandbox Code Playgroud)
...我在我的活动中注册它:
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"));
Run Code Online (Sandbox Code Playgroud)
当我运行应用程序时,我的服务发送广播,但我的活动没有收到广播消息!
我认为这个问题是因为我的服务是在我的清单中的另一个进程中定义的......
android:name=".services.ForegroundService"
android:enabled="true"
android:exported="false"
android:process=":ForegroundService"
Run Code Online (Sandbox Code Playgroud)
...但我想以这种方式进行交流,因为处于不同的过程有助于我实现电池效率目标。
如何在不同进程中使用 LocalBroadcastManager 在 Activity 和 Service 之间进行通信
这不可能。中的“本地”LocalBroadcastManager表示“本地到此进程”。LocalBroadcastManager特别是在进程之间不起作用。
任何一个:
让活动和服务处于同一进程中,或者
使用某种形式的 IPC 在进程之间进行通信(系统广播Messenger等)
| 归档时间: |
|
| 查看次数: |
243 次 |
| 最近记录: |