8

I know that an activity can communicate with a local service using the IBinder interface; I am trying to find a way for communication between two services.

Specifically, I have my main service starting an IntentService to handle file uploads. I want this IntentService to inform back to the main service once it is done uploading, and before it dies.

Any ideas about how this would happen?

Chaitanya
  • 2,039
  • 4
  • 25
  • 32

2 Answers2

21

You have to use BroadcastReceiver to receive intents, and when you want to communicate simply make an Intent with appropriate values.

This way you should be able to make a 2-way communication between any component.

user229044
  • 232,980
  • 40
  • 330
  • 338
Tofeeq Ahmad
  • 11,935
  • 4
  • 61
  • 87
  • I was looking for something on the lines of IBinder - my activity communicates via it. But I will go ahead and implement bacstReceiver.. Thanks! – Chaitanya Apr 21 '11 at 14:03
  • ok..dude actually i am not sure about it is possible with IBinder or not. – Tofeeq Ahmad Apr 21 '11 at 14:17
  • AFAIK there is no IBinder equivalent between services. – Jon Willis Apr 21 '11 at 14:18
  • 1
    I request you to rate my answer if you like ..im new on stackoverflow – Tofeeq Ahmad Apr 21 '11 at 14:23
  • sure - I have accepted your answer. I will actually get rid of IBinder and go with broadcastReceiver for all the communication, simplifying my code. :) – Chaitanya Apr 21 '11 at 15:02
  • 1
    There's nothing preventing you from calling `bindService()` from within one service in order to bind another to it. – Avi Cherry Jun 30 '15 at 01:57
  • you cann't bind intent Service. And there is only one way to communicate with Intent Service which is Receiver – Tofeeq Ahmad Jul 04 '15 at 12:00
  • @Sameer Thats actually not true at all, you can use Messengers, Messages and Handlers, which is less expensive than a Broadcast Receiver - but hey each can be used depending on your needs ... – Mark Apr 04 '16 at 23:28
4

In Android, there is a special way of completing tasks like yours. Look at AIDL (it's not well documented in official docs, but there are some extra sources on the web). This is a way of implementing two-way communication between any components placed in separate processes. In comparison to BroadcastReceivers, using this you'd get direct calls and callbacks, that will be less dirty than relying on something would come from somewhere in BroadcastReceiver.

To reach the needed effect, you'll have to define an interface for a callback and an interface for performing actions (with a callback supplied, or register/unregister methods). Than, after you received some command using the second interface, you should perform the job and post back the result through callback. To reach the asynchronous completion add a key work "oneway" before method signature (return type). To separate in and out params (if you need it), use "in", "out" and "inout" keywords near params.

As it comes to restrictions, only primitives, arrays and parcelables (and parcelable arrays) might be transferred between processes.

To control your callbacks lifecycle and operations atomicity, use RemoteCallbacksList for storing registered callbacks and notifying recipients using the duplicate of your list got from beginBroadcast.

If you have any troubles, you're free to ask here.