33

I seem to have a classic task, yet I can't find any examples on how to do it.

I want to download something. Well I call a web service and get a response... but it's pretty much the same.

In order to do this I have an activity that starts a service that spawns a thread that does the job.

Now i would like to use the data I got in the activity that started the service.

(I assume that starting another activity to handle the job of displaying the result would be simple)

My problem is how does the service notify an activity (the one that started it or another one) of something?

Any pointers to examples are much appreciated.

Regards

Gabe
  • 5,997
  • 5
  • 46
  • 92
Alex Poke
  • 535
  • 2
  • 7
  • 16
  • You can use local broadcast receiver for this check this simple local broadcast example here http://wiki.workassis.com/android-local-broadcast-receiver/ – Bikesh M Jul 15 '16 at 07:32

4 Answers4

62

According to Google documentation, if your Activity and Service are within the same app, using a LocalBroadcastManager is preferable over sendBroadcast (intent) because the information sent does not go through the system which eliminates the risk of interception.

It's quite easy to use.

In your activity, create a BroadcastReceiver and dynamically add a listener in the onResume() method :

private BroadcastReceiver  bReceiver = new BroadcastReceiver(){

    @Override
    public void onReceive(Context context, Intent intent) {
        //put here whaterver you want your activity to do with the intent received
    }           
};

protected void onResume(){
    super.onResume();
    LocalBroadcastManager.getInstance(this).registerReceiver(bReceiver, new IntentFilter("message"));
}

protected void onPause (){
    super.onPause();
 LocalBroadcastManager.getInstance(this).unregisterReceiver(bReceiver);
}

And in your service, you get an instance of LocalBroadcastManager and use it to send an intent. I usually put it in its own method like this :

private void sendBroadcast (boolean success){
    Intent intent = new Intent ("message"); //put the same message as in the filter you used in the activity when registering the receiver
    intent.putExtra("success", success);
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
Gautam Chibde
  • 1,167
  • 3
  • 14
  • 27
JDenais
  • 2,956
  • 2
  • 21
  • 30
  • This is much more helpful than accepted answer in terms of work-able code – Ben Schmidt Jun 18 '17 at 18:55
  • how do I pass intent to my service at first place from the activity using this implementation? @JDenais – Itay Braha Jun 12 '19 at 09:31
  • 1
    @ItayBraha the Intent passed to the LocalBroadcastManager is created by the Service. The Activity starts a service with an Intent using the classic `startService(Intent)` Activity method. – JDenais Jun 12 '19 at 10:43
  • LocalBroadcast is deprecated now: https://developer.android.com/reference/androidx/localbroadcastmanager/content/LocalBroadcastManager – juztcode Mar 06 '23 at 14:36
9

Send a broadcast Intent with the data via sendBroadcast(), that the activity picks up with a BroadcastReceiver.

Here's an example of that: https://github.com/commonsguy/cw-android/tree/master/Service/WeatherAPI Doc: http://developer.android.com/reference/android/content/BroadcastReceiver.html

edi9999
  • 19,701
  • 13
  • 88
  • 127
fpanizza
  • 1,589
  • 1
  • 8
  • 15
4

If you need return various types of data, such as user-defined class, you may try bindService and callback. This can avoid implement parcelable interface than sendBroadcast.

Please see the example in my post:

https://stackoverflow.com/a/22549709/2782538

And more, if you use IntentService, you could look into ResultReceiver. The details and sample could be found in this post:

Android: restful API service

Hope it helps.

Community
  • 1
  • 1
Euporie
  • 1,896
  • 1
  • 21
  • 23
3

You can use Pending Intent,Event Bus,Messenger or Broadcast Intents to get data from service back to activity/fragment and then perform some operation on the data.

My blog post covers all these approaches.

Android Developer
  • 9,157
  • 18
  • 82
  • 139