Application, Service and AIDL

Our demonstration Android application for Leedshack came in two parts – a user-facing application which the user could use to drop “markers” (points used to attach triggers to) and a background process which checked a remote web service to look for any triggers to fire.

So I don’t repeat anything which is already covered elsewhere – here’s the Android documentation on services. It’s good. Go read it.

Since we were looking to implement the multiple client architecture, we decided to use the Messenger class to pass messages between the application and the service. Messages can be of simple types (int, etc) or can be complex serialised classes. These serialised classes must inherit from the Parcelable interface. The writeToParcel and Constructor define the serialisation/de-serialisation. The interesting CREATOR static property allows the system to work with the parcelable object.

There’s one other part to making this magic work. It is in the documentation but you may overlook it. You require an AIDL file with the parcelable definition in. AIDL files are converted at compile time by the aidl/aidl.exe tool to java class files. They’re also included into the built assembly by some form of monkey magic which the system uses to recognise the parcelled type. Your AIDL file sits alongside your .java file and should look something like this:

package com.orangetentacle.androidsample;

parcelable MyParcelableType;

Congratulations. After all the above, your service should now be able to talk to your application across processes.