Android - Unable to receive local broadcast in my activity from service

前端 未结 3 1619
既然无缘
既然无缘 2020-12-12 02:33

I have my main activity that start a service (Location service) and I want that service to broadcast the new location each time a new location is found.

Thanks to th

相关标签:
3条回答
  • 2020-12-12 03:31

    In your LocationService, send local broadcast using:

    Intent intent = new Intent(CMBroadcastReceiver.RECEIVE_LOCATION_UPDATE);
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    
    0 讨论(0)
  • 2020-12-12 03:31
    <service android:name=".LocationService" android:process=":location_service" />
    

    Your service is in a separate process from the activity. LocalBroadcastManager is only for use in one process. Either remove android:process from the <service>, or use some IPC mechanism (e.g., system broadcasts, properly secured).

    0 讨论(0)
  • 2020-12-12 03:32

    LocalBroadcastManager does not work across processes. Your Service is running in a separate process.

    You can either run your Service in the same process as the Activity - by removing the process attribute from the <service> element - or use some sort of IPC instead - e.g., by sending and receiving the broadcasts on a Context instead of LocalBroadcastManager.

    0 讨论(0)
提交回复
热议问题