How can I set up my Activity to be
listening to the Service? Is this the
best way to approach this problem?
You have three major options, as I see it:
-
Polling. The
Activityperiodically asks theServicefor the latest data. IMHO, this option sucks, but it’s certainly possible. -
Callbacks. Per jax’s answer, the
Activityregisters a callback object (“observer”) with theService. TheServiceinvokes a method on the callback when the data changes, which in turn updates the UI. You can see an example of using that with aServicehere. -
Broadcast
Intents. TheServicebroadcasts anIntentviasendBroadcast()on a data change. TheActivityregisters aBroadcastReceiverusingregisterReceiver(), and thatBroadcastReceiveris notified of an incoming broadcast. This triggers theActivityto load the latest data from theService, or possibly just to get the latest data out of extras in the broadcastIntent. You can see an example of using that technique with aServicehere.