Android AIDL Example with Code Description – IPC

android-logoBefore we learn about Android Interface Definition Language (AIDL), Let us know why it is required? The answer is very simple. Each Android app run in it’s own process. So one application can’t directly access another app’s memory space. If you want to access some data from one application to another application, then we need to use  inter process communication (IPC) like other platforms. So in Android IPC is otherwise known as AIDL. For this purpose Android provides a technique called Android Interface Definition Language (AIDL). It is a lightweight implementation of IPC just like C/C++ languages.

So to communicate between process or you say between applications, we need to implement AIDL in our Android application. In other words you can say the AIDL technique is a light weighted client server architecture in form of a service, which helps multiple applications to communicate between them self.  To do so we need the below steps.

1. AIDL interface- Define a AIDL interface which will be the interface between your applications.
2. Implement the the remote service- Remote service holds your data/method to expose to other applications for accessing data.
3. Expose the remote service to other local clients- The data/method we need to share with other applications needs to be expose so that other application can access and share data from the remote service.

To make the example simple, lets take an simple example. We will develop a remote service for addition of two numbers. That means we will get user input from client application, then send the data to remote service and get the result back from remote service. I thing it is good enough to learn basic of remote server using AIDL.

aidl-example-in-android

Creating our AIDL interface- Define a AIDL interface which will be the interface between your applications

here we just define the method signature (prototype), which we need to expose to other clients(apps). please note that  all Java primitive datatypes are supported in AIDL. You can also use your own user define datatypes in AIDL. Also you can use Parcelable classes here. but here we will discuss with basic primitive datatypes to make the example simpler. So below is the code snippet.

Put the below file in your source folder like this: \src\com\techblogon\aidlexample\IAddService.aidl

Interface File: IAddService.aidl

Note: In the above code  the method add() will be exposed to local clients.

Implementing our Remote Service – 

After we created our AIDL file and put it in proper place as above, the inbuilt AIDL tool in Eclipse will generate a new file with ‘.java’ extension with the same name of your AIDL file, which your can find in your project’s ‘jen’ folder. So now we have a new file called  ‘IAddService.java’  in the location \gen\com\techblogon\aidlexample. This is an auto-generated file so don’t edit it at all. The mail purpose of this calls is that it contains a Stub class that we need to implement for our remote service.

To implementing our remote service, We need to create a new service class by extending the Android Service class (AddService in our case). Then we will return IBinder from onBind() method in our service class, which represents the implementation of our remote service. To implement IBinder, we need to subclass IAddService.Stub class from the auto-generated Java code. Then we need to implement the code logic for our exposed method called add() in our case, which we just declared in the AIDL interface file. Then his method add() will be called from client side code. Have a look into the below code snippet.

Put the below file in your source folder like this: \src\com\techblogon\aidlexample\AddService.Java

Remote Service File: AddService.Java

 Exposing our Remote Service to Local Clients

After we implemented the onBind() properly in our service class as above, now we are ready to connect to our service from local client. For this we will have a simple Activity class from where we will connect to our remote service and call the exposed  add() function from the service . To establish the connection between service and local client, we need to create a new inner class by implementing Android ServiceConnection class in side our activity class (AIDLActivity in our case). So our  AIDLActivity class  implemented AddServiceConnection  inner class by implementing onServiceConnected() and onServiceDiconnected()  methods. These methods are the callbacks, which will get the stub implementation of the remote service on connection. Then we just need to type cast them from our Stubs to our AIDL service implementation. So to do so, we need to use the IAddService.Stub.asInterface((IBinder) boundService) helper method. From here we will have a local service pointer to accessing the data and methods. So initService() is our method which is called from Activity’s onCreate() method to established the connection. Then when we click on ‘Result’ button the onClickButtonResult() method will be call and we have called our remote service’s add() method there.

Put the below file in your source folder like this: \src\com\techblogon\aidlexample\AIDLActivity.Java

Local Client  Activity File: AIDLActivity.Java

XML Layout (activity_aidl.xml)

Our UI contains simple elements. We have used 2 EditText for getting user input and one button for getting result. The result field is also an EditText with editable false. You can use any other controls. Also we have used a Textview for showing some informations ad hints.

Manifest File

In the final step we wll put our service class entry (AddService in our case) in our project’s Manifest file like below.

That’s it. Build and run the application. Enter 2 digits and click on Result button, you can get the result from the AIDL servie.

download Download complete sample code (AIDL Eample) from here.

I hope this small tutorial on Android AIDL will help you at it’s best.

 

Post By SmrutiRanjan (57 Posts)

Working @ Samsung as a Project Lead for Android Smartphones. I have been blogging since 2008. Previously I was writing articles for other bloggers, but finally I have started my own blog-"Techblogon".I am also an active contributor for the blog-"Gadgets n Gizmos World". Job is my necessity, but blogging is my passion.

Website: → Techblogon

Connect

2 Responses to Android AIDL Example with Code Description – IPC

  1. Anil says:

    Hi,

    Can you please tell me where we will use AIDL in real time appalication.

    Thanks.

  2. Dennis says:

    Good Article. i like your article.

Leave a Reply

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


four × 1 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">