I have an Activity that starts and binds to a service. It sends an intent with a List of data and the service is responsible for periodically updating that data. This is done in the handleIntent (Intent) method. What I want to do is send the updated data back to the activity. Both the activity and the service are in the same application. How can I "listen" for requests from my service? Do I have to use a Messenger and/or Broadcast Receiver? what's the cleanest, easiest, most efficient way of doing this? Thanks.
Asked
Active
Viewed 465 times
2 Answers
2
I suggest you check EventBus. it will allow you to send events like local broadcast receiver. And from that in activity you can listen specific event and display result. you can even use interface to get callback from service also.
Without any library you can achieve this with LocalBroadcastManager. http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html

Wasim K. Memon
- 5,979
- 4
- 40
- 55
-
I should've mentioned that I'm looking for a "stock" way of doing this without external libraries - something that's built-in to Android. – Sina Madani Feb 14 '16 at 18:58
-
That seems like it would do the job but for some reason I can't import it. I can import android.support.v4.content.* but not android.support.v4.content.LocalBroadcastManager. – Sina Madani Feb 14 '16 at 19:20
-
@SinaMadani For that I think you have to add support v4 library in gradle. As package suggest it is in v4 content package so. – Wasim K. Memon Feb 15 '16 at 03:13
0
For this problem, you can use a ResultReceiver. If both the service and activity share the same ResultReceiver object then you can "push" data to the Activity from the Service using the "send(int, Bundle)" method and on the Activity side receive it with "onReceiveResult(int, Bundle)".

Sina Madani
- 1,246
- 3
- 15
- 27