Android Service: Interprocess Communication With AIDL


Photo Credit: Niels Linneberg via Compfight cc

Android services are designed for many permutations and combinations. You can use them for long running operations, you can use them for long running network operations, you can use them for never ending operations in a separate process. One of these many permutations and combinations is Android service interprocess communication through the usage of AIDL. The Android Interface Definition Language. In this tutorial I would show you how to create an interface through AIDL and use it for interprocess communication between a service an activity.

Before starting with the tutorial let me put down some facts for beginners.

  1. Android services are not background tasks, they run on your app’s main thread only.
  2. An Android service can be started in a different process, which would free your app’s main thread.
  3. If an Android service is started in a different process, you cannot communicate with that service through a normal IBinder interface.

To do so we need to establish an inter process communication between the android service (in process A) and the activity (in process B). One of the two ways of establishing interprocess communication (IPC) is through, the use of AIDL. Although it sounds a little complex, but believe me its not that difficult. All you need to do is, create an interface, like a normal java interface. Then place it in your file structure with an extension .aidl. Just like the image shown below.

Android Bound Service AIDL

AIDL : Android Interface Definition Language

To perform IPC (Inter Process Communication) in Android, one needs to define a set of methods through which a process can be accessed from a remote process. Through AIDL we can define these methods. AIDL is just like any other interface in Java, where abstract methods are defined. To do so we need to create a file with .aidl extension and define all the abstract methods in it, which are to be accessed outside this process.

One of the main features of AIDL is that, by using this we can communicate between two applications. Although if you don’t need to communicate with a different app (in a different process of course), this approach should not be used. As this feature also imposes a direct restriction on the implementation. That only a primitive data type, and some other basic data types like, string, lists, maps, etc. can be used as parameters for methods. If you want to use a custom class object as a parameter, then you need to implement the Parcelable interface in that object’s class and import it in the AIDL too. This can be a separate tutorial in itself, therefore for the sake of simplicity I have not included it in this Android Service Interprocess Communication With AIDL tutorial.

While designing the Android service which will implement this AIDL interface, keep in mind that this service can be accessed by more than one application at a time. Therefore you need to implement this service as a multi-threaded service, assuming any method defined in this service can be executed simultaneously by more than one application.

Android Inter Process Communication In Same App

In this Android Service: IPC With AIDL tutorial, my aim is to get the running time of an Android service in an activity. To do this I will start an Android service in a process, with a timer running inside it. Then I will bind it to an activity in a separate process. I will be doing all of this, in the same app. As in normal scenarios, mostly when a service is started, it is accessed in the same app first. Although if it needs to be accessed in a different app, it will be accessed in the same way. Moving on lets have a look at the code:

In the above manifest, you can see highlighted, the service is started in a separate process. Reason being we have to perform Android Service Interprocess Communication With AIDL. Next lets define the Android AIDL file.

After creating this file with .aidl extension, you need to put this file in the /src folder of your project. After this, Android SDK tools will automatically generate an IBinder interface in your /gen folder. You can access this interface through a Stub subclass, which will also be created automatically. Have a look at the service code below:

As you can see the above implementation of Android service is very basic, its running a Chronometer to keep track of the service start time. In this Android service inter process communication with AIDL example my aim is to get the running time of this service in an activity. Now if you remember this service is running in a separate process, therefore to get the running time of this service out of this process, we need to perform inter process communication. Hence getTimestamp() method defined in the AIDL, is implemented here.

Android Bound Service AIDL

Next lets have a look at the layout file of the activity, where this method would be accessed:

After rendering this layout, it would look something like this:

Android Bound Service AIDL

This layout would be inflated in MainActivity.java, lets have a look at the code for this class:

As you can see in the above class, an android service by the name of BoundService is started by calling startService() method. Further the same service is bound by calling the bindService() method. But here the binding process is a little different from standard Android bound service. Here if you see closely IBoundService.Stub.asInterface(service) is used to get the interface in this activity, through which the getTimestamp() method is called, which prints the time stamp. To get a better understanding, have a look at the full source code below:

Full Source Code

For the sake of simplicity in this Android Service Interprocess Communication With AIDL tutorial I have used the same app to access the service which was started in a different process. But if you are trying to access the service in a different app, don’t forget to add the .aidl file in that app’s /src folder structure. By doing this you would be able to access the remote service’s methods. Hope this helps.

About Mohit Gupt

An android enthusiast, and an iPhone user with a keen interest in development of innovative applications.


Leave a comment

Your email address will not be published. Required fields are marked *

2 thoughts on “Android Service: Interprocess Communication With AIDL