While doing Android programming, it's a common sense to use activity to interact with users and use service to perform time consuming tasks (actually, such tasks are usually saved med on a new thread spawned by the service ). A classic example is media player. it's composed of several activities and a playing-back service. through activities, users can pick a media file to play, or control (START, pause, fast forward, set volume, etc) the playing-back service. and the service is responsible for playing the media file. the service can run in background even no activity of the player application is active. it's also possible that the service is implemented and hosted in
Another ApplicationOther than the one where activities are implemented.
In order to allow activities to control the behavior of the service, there shocould be an mechanic to allow they talk to each other. and by following the process agnostic feature of Android platform, this communication mechanic shoshould also be able to handle
Inter-processAnd
Inside-ProcessCommunication consistently. Such that from the upper layer application code's point of view, it's making communications between components
IndependentWhich process these components run in.
Luckily, Android has already provided such a RPC mechanic which has been briefly introduced here: Android Application fundamentails-Remote Procedure CILS. And the document
Designing a remote interface using aidlAlso shows a tutorial about how to perform RPC.
But the documents above don't include a complete yet simple example for us to follow, so I 'd like to know e how to do inside-process as well as inter-process with an example.
This example is composed of two applications. the first application (app1) will bind to a service runs in another application (app2) when a button is clicked, thus demonstrates inter-process communication. app2 can bind to the service too, which demonstrates inside-process communication.
There are two buttons on each application's activity. First, you need to click button titledStart SVCTo bind to service and get a reference to binder object. Then, you can click button titledCall RPCTo invoke a method in service.
It's trivial to repeat the tutorial of designing remote interface. So I 'd like to just post some findings during my registration.
- The onbind method of service shoshould returnNon-nullIbinder. Otherwise, the callback function onserviceconnected won't be fired.
- Add two aidl interface withSame nameTo both projects (isvccontroller. aidl in our example ). and the interface can be used in both projects to manipulate the ibinder object returned by the Service. these two interfaces can have different member functions, but any function to be used must haveSame signatureAnd be placed atSame place (INDEX)In these interfaces.
- When access the service in the same process, the original ibinder returned in onbind method is passed to onserviceconnected callback method. when access the service in a different process, a proxy for the ibinder is passed to onserviceconnected callback method.
- It's necessary to call unbindservice to avoid connection leaking. this can be done upon specific event handler such as a button click. A better choice is to unbind in one of activity's life cycle events, for example, onstop. so that the connection will be freed no matter in which way the user leaves the activity.
Source code
Browse code here:
Http://code.google.com/p/Rxwen-blog-stuff/source/Browse/# SVN/trunk/Android/RPC-With-Service
Or via command:
SVN Co https://rxwen-blog-stuff.googlecode.com/svn/trunk/android/rpc-with-service