1

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.

Mahima MS
  • 153
  • 1
  • 10
  • What are you returning from `onBind()` now? Do your clients use AIDL to call methods in the `Service` now? – David Wasser Dec 07 '20 at 12:45
  • Since you want to send data from `ServiceA` to `ServiceB`, you should only need to implement the `Messenger` API in one direction (ie: `ServiceA` will bind to `ServiceB` and be a client of that `Service`, not the other way around). Or am I missing something? – David Wasser Dec 07 '20 at 12:47
  • @DavidWasser ServiceA's existing clients include Activities and other Services from App1. `onBind` returns a binder created from extending the `Binder` class. – Mahima MS Dec 08 '20 at 04:44
  • @DavidWasser App2 is new and will have ServiceB that needs to exchange data with ServiceA. ServiceA will expose a binder using the `Messenger` framework. - This is clear to me. Existing clients of ServiceA in App1 bind to it and call some methods on it directly. If ServiceA is to use `Messenger`, my understanding is that the existing clients will no longer be able to invoke the methods directly but have to create a `Messenger` object using the binder they receive in `onServiceConnection` and pass `Message`s whenever they need to exchange/get data from ServiceA. Is that right? – Mahima MS Dec 08 '20 at 04:52

1 Answers1

0

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!

David Wasser
  • 93,459
  • 16
  • 209
  • 274