All CategoriesAndroid App Development

Android AIDL: Service-to-Activity Communication

Introduction to AIDL:

AIDL (Android Interface Definition Language) is similar to other IDLs you might have worked with. It allows you to define the programming interface that both the client and service agree upon in order to communicate with each other using interprocess communication (IPC).

On Android, one process cannot normally access the memory of another process. So to talk, they need to decompose their objects into primitives that the operating system can understand, and marshall the objects across that boundary for you. The code to do that marshalling is tedious to write, so Android handles it for you with AIDL.

Use of AIDL:
Using AIDL Services, functionality can be shared among multiple applications.

If we want to access the data from one application to another application, then there is a need of Inter-Process Communication (IPC) and in Android it is AIDL.

It is a light weight Client Server Architecture in form of a service.

Why do We Need AIDL?
If we want to communicate between multiple applications, then there is a need for AIDL interface.

Let’s explain it with the help of an Example:

In this example we add two numbers as a client side then it sends the data to service and gets back with the result.

To create AIDL interface follow the steps:

Example with code

⦁ Create an Application AndroidAIDLServiceExample.
Requirements:
IAddService.aidl
AddService.java
MainActivity.java
activity_main.xml
Register your Service in AndroidManifest.xml file.

IAddService.aidl :
Create this interface file in your application src folder.
Add the code inside this file.

package com.example.androidaidlserviceexample;
interface IAddService {
int add(in int ValueFirst, in int valueSecond);
}

ADT creates AIDL implementation file in “gen” folder, having an abstract class named “Stub”.
Stub class inherits from Binder (Android.OS) and implements IAddService.

Android Programming

AddService.java:
Now we create the class which extends service. Inside this implement the stub method and return stub object from onBind() method.

Add the code of onBind() method inside this service.

@Override
public IBinder onBind(Intent intent) {
// returns the stub because Stub extends Binder(which implements IBinder)
return new IAddService.Stub() {
public int add(int ValueFirst, int valueSecond) throws RemoteException {
Log.i(TAG, String.format("AddService.add(%d, %d)",ValueFirst, valueSecond));
return (ValueFirst + valueSecond);
}
};
}

activity_main.xml:
Create the main layout file
Add the code

<TextView
android:id="@+id/tv_Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Enter 2 digits for addition, then click result button for getting the result form the service using AIDL."
android:textColor="#3393ff"
android:textSize="20dp"
android:textStyle="bold" />
<EditText
android:id="@+id/ed_FirstNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_Title"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:ems="10"
android:hint="Enter a number"
android:inputType="number" />
<EditText
android:id="@+id/ed_SecondNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ed_FirstNumber"
android:layout_centerHorizontal="true"
android:layout_marginTop="33dp"
android:ems="10"
android:hint="Enter a number"
android:inputType="number" />
<EditText
android:id="@+id/ed_Result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ed_SecondNumber"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:editable="false"
android:ems="10" />
<Button
android:id="@+id/btn_Result"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_below="@+id/ed_Result"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:background="#ADD8E6"
android:onClick="onClickButtonResult"
android:text="Result" />

MainActivity.java:
In our main activity we create an inner class i.e AddServiceConnection which implements ServiceConnection having two methods onServiceDisconnected() and onServiceConnected().

To establish the connection Add the code in onCreate(){…}:

connection = new AddServiceConnection();
Intent i = new Intent();
i.setClassName("com.example.androidaidlserviceexample", com.example.androidaidlserviceexample.AddService.class.getName());
boolean ret = bindService(i, connection, Context.BIND_AUTO_CREATE);

Add the code in inner class onServiceConnected() method:
To get service instance in interface, use Stub.asInterface in onServiceConnected() method.

Use IAddService as reference and call add() method.

Since IAddService is reffering AddService, add() method will be called from AddService.

public void onServiceConnected(ComponentName name, IBinder boundService) {
service = IAddService.Stub.asInterface((IBinder) boundService);
Log.i(TAG, "onServiceConnected(): Connected");
Toast.makeText(MainActivity.this, "AIDLExample Service connected", Toast.LENGTH_LONG).show();
}
Add the code of Calculation in onClickButtonResult() method:
TextView value1 = (TextView) findViewById(R.id.ed_FirstNumber);
EditText value2 = (EditText) findViewById(R.id.ed_SecondNumber);
EditText result = (EditText) findViewById(R.id.ed_Result);
int n1 =0, n2 =0, res = -1;
n1 = Integer.parseInt(value1.getText().toString());
n2 = Integer.parseInt(value2.getText().toString());
try {
res = service.add(n1, n2);
} catch (RemoteException e) {
Log.i(TAG, "Data fetch failed with: " + e);
e.printStackTrace();
}
result.setText(new Integer(res).toString());

Output

Download Project File: GitHub

Conclusion:
This was all about the AIDL service in Android. It is nothing but an interface which is used to communicate between the applications. Like this example, we added two numbers and got the result with the help of Service.

It is a very important feature of Android, which is used in many applications and is quite popular with Android developers.

Hope you find this Blogspot helpful and keep visiting www.acadgild.com for more updates on the courses.

Android Programming

Tags

2 Comments

  1. I was hoping to download this project so I could take a look at the running code. Your github link doesn’t have a download option

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Close
Close