Search code examples
androidandroid-serviceipcandroid-binderandroid-messaging

Android Bound services: Inter vs Intra - app communication using Messenger


I have 2 Android apps - App1 and App2. I have a bound service - ServiceA in App1. Multiple services and activities in App1 bind to ServiceA and call methods on it. Now, I want to send data from ServiceA to a remote service that exists in App2. I will be using the Messenger API to expose the binder object from ServiceA for inter-process-communication.

From what I understand, all the activities and services dependent on ServiceA in App1 will also now need to use the Messenger API to access the binder. Is this correct?

If yes, is there a way to make changes only to ServiceA so that it can exchange data with the remote service without making changes to it's existing clients?

P.S: The service doesn't need to handle multiple concurrent requests which is one of the main reasons I decided to go with the Messenger API.


Solution

  • You should be able to provide both a Messenger based interface and a direct interface. I've not tested this myself, but you can try this:

    In onBind() you receive an Intent. This is the Intent that the client used when calling bindService(). You can use 2 different ACTIONs (or use "extra"s) in the Intent so that you can differentiate between the calls from App1's clients and App2's clients. Then just return either a Messenger based Binder or your current implementation, depending on which client has called onBind().

    Let me know how it goes!