Java 如何在不同流程中使用LocalBroadcastManager在活动和服务之间进行通信

Java 如何在不同流程中使用LocalBroadcastManager在活动和服务之间进行通信,java,android,android-activity,android-broadcast,localbroadcastmanager,Java,Android,Android Activity,Android Broadcast,Localbroadcastmanager,我有一个在清单和MapboxMap活动中定义的不同进程中的服务,我只是想知道如何使用LocalBroadcastManager在我的服务和活动之间进行通信 我已尝试将服务上下文传递给LocalBroadcastManager.getInstance,然后在活动中注册LocalBroadcast。它成功注册,但无法从我的服务中获取信息 这是我的代码,为我服务 Intent locationIntent = new Intent("LocationIntent"); location

我有一个在清单和MapboxMap活动中定义的不同进程中的服务,我只是想知道如何使用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"));
…但我希望通过这种方式进行沟通,因为处于不同的流程中有助于我实现电池效率目标

如何在不同流程中使用LocalBroadcastManager在活动和服务之间进行通信

这是不可能的。LocalBroadcastManager中的本地表示此进程的本地。LocalBroadcastManager在进程之间不起作用

要么:

使活动和服务处于同一流程中,或

使用某种形式的IPC在进程、系统广播、信使等之间进行通信

如何在不同流程中使用LocalBroadcastManager在活动和服务之间进行通信

这是不可能的。LocalBroadcastManager中的本地表示此进程的本地。LocalBroadcastManager在进程之间不起作用

要么:

使活动和服务处于同一流程中,或

使用某种形式的IPC在进程、系统广播、信使等之间进行通信

android:name=".services.ForegroundService"
            android:enabled="true"
            android:exported="false"
            android:process=":ForegroundService"