2

Situation:

I have an Android System Application (persistent) with a Service that is supposed to run all the time. (Let's call it Service A)

Then there is another Application (not a System Application; normal App) that also has a Service. But this Service might run or might not run since the Android OS will kill it if it needs resources. (Let's call it Service B)

In short: We have two Applications (hence 2 APKs) that can be signed with the same certificate. (So they could run in the same process right?)

What I try to accomplish

The Service A listens to events. When it receives an event then I want the Service A to call the normal App (Service B) which should give a response back to Service A.

Approaches to let the two Services communicate

I could let them communicate using:

  • Binding (using an AIDL)
  • Messaging (but not optimal since passing complex objects is a must)

Problems

  • Which way to communicate is "the best" one for the described case?
  • What happens when Service A wants to delegate/send a message to Service B and Service B is not running? It must get a response from Service B. Should I always start Service B if it is not running when Service A wants to call it?

Thank you!

sjkm
  • 3,887
  • 2
  • 25
  • 43
  • if you want it fast and complex data to be passed see [this](http://stackoverflow.com/a/33688843/2252830) – pskink Jun 14 '16 at 20:10

2 Answers2

2

Which way to communicate is "the best" one for the described case?

You only cite two options, since "Aidl" and "Binding" are the same thing. You use AIDL to describe and implement the client-side proxy and the server-side stub for cross-process service binding.

Given that you want call-and-response, binding is probably a better choice. Note, though, that there is no IPC approach that supports "complex objects" very well, outside of using Parcelable, Serializable, or converting the object graph into JSON or something.

What happens when Service A wants to delegate/send a message to Service B and Service B is not running?

When you call startService() or bindService(), Android forks a process for App B and creates an instance of the service (invoking its onCreate()) method. At that point, the behavior is the same as if the process and service had already been running.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thank you very much for your informative answer - I appreciate it! So you would bind the Service and use an AIDL to describe the API? – sjkm Jun 13 '16 at 21:14
  • @sjkm: Yes. If you want a synchronous response, like a regular method call, have your AIDL interface define a return value on the method. If you want an asynchronous response, define AIDL both for Service B and for a callback object that will be passed from A. Then, pass an instance of the callback object in the AIDL-defined method, and B can use that to call a method on A's callback to provide the asynchronous results. See [this sample app](https://github.com/commonsguy/cw-omnibus/tree/master/Binding/Callback) for an example of the callback approach. – CommonsWare Jun 13 '16 at 21:24
  • Thank you! That looks great. I will have a deeper look at it tomorrow. Have a nice evening. – sjkm Jun 13 '16 at 21:43
  • I'd like to ask you a last question: A messenger (without AIDL) would only work for the same application (but remote processes), right? – sjkm Jun 13 '16 at 22:28
  • @sjkm: You could use that for the callback, as opposed to a custom AIDL-defined callback. I don't see what it gains you. – CommonsWare Jun 13 '16 at 22:30
0

If application containing Service A is a System Application then how these two apps can be signed with the same certificate ?

Do you have the access to platform certificate?

Anyways aidl will be a good option since you want a response from service B.

Arpit Ratan
  • 2,976
  • 1
  • 12
  • 20
  • You do not need to sign an App with the platform certificate in order for it to be a System Application. It just has to be installed in the "/system/app" / "system/priv-app" folder... – sjkm Jun 13 '16 at 21:16