Reputation: 1183
I have an Android app, with two components
Please can someone tell me the simplest way to pass the location data from the service back to the activity that will work whether or not the activity is displayed to the user.
Should I use EventBus? Broadcast Receiver, Local Broadcast Manager or what?
Ideally I would like to be pointed in the direction of an example app on GitHub (or similar) that I can download an examine how it works, failing that some code would be nice.
Upvotes: 1
Views: 1488
Reputation: 1183
I found the easiest solution was in fact using EventBus. Their website getting started page states four simple steps
this works well even when the main activity is paused (in the background)
Two little extras things to do, that were not documented in the link above.
Upvotes: 1
Reputation: 1594
Since you are using a Bound service
, the simplest way would be to return an appropriate Binder
object when the Activity
binds to the Service
.
The documentation for that is in here.
Also, this is the sample code for a bound service and the corresponding actvitiy.
Note that the Activity
, once connected, is able to call the public methods that were added to the corresponding Binder
objects. If you want the Service
to provide info to the Activity
at its own discretion, simply register a listener that the service could invoke.
Upvotes: 0