[Solved] How to use PendingIntent to communicate from a Service to a client/Activity?

I have been reading the following text on the Android Developers Site, specifically under the Framework Topics -> Services -> Starting a Service.

There it states the following :

If the service does not also provide binding, the intent delivered with startService() is the only mode of communication between the application component and the service. However, if you want the service to send a result back, then the client that starts the service can create a PendingIntent for a broadcast (with getBroadcast()) and deliver it to the service in the Intent that starts the service. The service can then use the broadcast to deliver a result.

I have a couple of questions regarding this :

  1. Does this text both apply to Services and IntentServices ?
  2. How (codewise) should this be achieved from within the Service; The service can then use the broadcast to deliver a result. and also where would the mentioned broadcast deliver the result to the original client/activity? Is there some method that should be overwritten (like onActivityResult()) or something?
Enquirer: TiGer


Solution #1:

Question was asked few months ago, but in case anyone is still looking for answer I hope I can help.

In the example below we have local service, responsible for performing some time-consuming operations. Activity makes the requests to the service, but does not bind to it – just sends the intent with request. Additionally, Activity includes the information of BroadcastReceiver that should be called back when service is done with the requested task. The information is passed by PendingIntent. The service handles the task in background thread and when task is finished, service broadcasts the BroadcastReceiver with an answer.

1. Create BroadcastReceiver subclass:

public class DataBroadcastReceiver extends BroadcastReceiver {
   static Logger log = LoggerFactory.getLogger(DataRequestService.class);   
   public void onReceive(Context context, Intent intent) {
      log.info(" onReceive");

This broadcast receiver will be notified from service, when task is done.

2. Create Service

public class DataRequestService extends Service {

   private final class ServiceHandler extends Handler {
      public ServiceHandler(Looper looper) {

      public void handleMessage(Message msg) {
         //... performing some time-consuming operation         
         Bundle bundle = msg.getData();
         PendingIntent receiver = bundle.getParcelable("receiver");
         // Perform the operation associated with PendingIntent
         try {            
            //you can attach data from the operation in the intent.
            Intent intent = new Intent();
            Bundle b = new Bundle();
            //b.putString("key", value);
            receiver.send(getApplicationContext(), status, intent);
         } catch (CanceledException e) {         
   public void onStart(Intent intent, int startId) {
      Bundle bundle = intent.getExtras();
      Message msg = mServiceHandler.obtainMessage();

Well, the most important part is in handleMessage() method. Service simply makes the broadcasts operation for delivering results to Broadcast Receiver.

3. You also need to register your broadcast receiver and service in Manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    android:versionName="1.0" >
       <service android:name=".service.DataRequestService" android:exported="false"/>
       <receiver android:name=".service.DataBroadcastReceiver"></receiver>

4. And finally, make request to your service from Activity:

Intent serviceIntent = new Intent(context, DataRequestService.class);   
   public void onClick(View v) {
      //this is the intent that will be broadcasted by service.
      Intent broadcastReceiverIntent = new Intent(context, DataBroadcastReceiver.class);      
      //create pending intent for broadcasting the DataBroadcastReceiver
      PendingIntent pi = PendingIntent.getBroadcast(context, 0, broadcastReceiverIntent, 0);      
      Bundle bundle = new Bundle();            
      bundle.putParcelable("receiver", pi);
      //we want to start our service (for handling our time-consuming operation)
      Intent serviceIntent = new Intent(context, DataRequestService.class);

5. Delivering response to original client/activity.

You can have abstract activity from which all your activities will be extending. This abstrct activity can automatically register/deregister itself as a response listener in broadcast receiver. Not many options here actually, but it is important that if you keep static references to your activity then you must remove the refernece when activity is destroyed.


Respondent: Ramps

Solution #2:

In order to perform communication between service and activity. You can also use Binder as mentioned in Official Android Example

For detail explanation see this answers

Respondent: shanraisshan

Solution #3:

As written here

Communication between service and Activity can be done using
PendingIntent.For that we can use
createPendingResult().createPendingResult() creates a new PendingIntent object which you can hand to service to use and to send
result data back to your activity inside onActivityResult(int, int,
Intent) callback.Since a PendingIntent is Parcelable , and can
therefore be put into an Intent extra,your activity can pass this
PendingIntent to the service.The service, in turn, can call send()
method on the PendingIntent to notify the activity via
onActivityResult of an event.


public class PendingIntentActivity extends AppCompatActivity
protected void onCreate(@Nullable Bundle savedInstanceState) {

PendingIntent pendingResult = createPendingResult(
100, new Intent(), 0);
Intent intent = new Intent(getApplicationContext(), PendingIntentService.class);
intent.putExtra("pendingIntent", pendingResult);


protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 100 && resultCode==200) {
super.onActivityResult(requestCode, resultCode, data);


public class PendingIntentService extends Service {

    private static final String[] items= { "lorem", "ipsum", "dolor",
            "sit", "amet", "consectetuer", "adipiscing", "elit", "morbi",
            "vel", "ligula", "vitae", "arcu", "aliquet", "mollis", "etiam",
            "vel", "erat", "placerat", "ante", "porttitor", "sodales",
            "pellentesque", "augue", "purus" };
    private PendingIntent data;

    public void onCreate() {

    public int onStartCommand(Intent intent, int flags, int startId) {

        data = intent.getParcelableExtra("pendingIntent");

        new LoadWordsThread().start();
        return START_NOT_STICKY;

    public IBinder onBind(Intent intent) {
        return null;

    public void onDestroy() {

    class LoadWordsThread extends Thread {
        public void run() {
            for (String item : items) {
                if (!isInterrupted()) {

                    Intent result = new Intent();
                    result.putExtra("name", item);
                    try {
                    } catch (PendingIntent.CanceledException e) {


Respondent: Android Developer

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.