Interprocess Communication in Android - Part I

Interprocess communication can be done in two ways in Android.

- Through AIDL(Android Interface Description Language)
- Through Service and Broadcast Receiver

First I am going to explain inter process communication through AIDL.

Android Interface Description Language(AIDL)

For implementing inter process communication(IPC) through AIDL, the main things that we need are,

- Create a AIDL file containing the functions, that we are going to implement in the Stub class in Service.

- Create a service, in which we need to implement all the the methods that we declare in the AIDL interface. Android will create a stub class and we need to define the methods here. Stub class is nothing but an object of the IBinder. IBinder is an interface for remote objects. It desribes the protocol for interacting with the remote objects.

- Then for calling the AIDL service, we need to implement ServiceConnection interface, for monitoring the state of the application service. This interface contains two methods
a) onServiceConnected() - Called when a connection to the service is established with IBinder of the communication channel.
b) onServiceDisconnected() - Called when a connection to the service has been lost.

In onServiceConnected(), method contains IBinder as an object and using that we can connect to the service and can invoke the methods of the AIDL interface. Android will also create a Java class for our AIDL interface class.

- Then like for all service, we need to declare the service in Manifest.

<service android:name=".RemoteService" />

Now, coming to our case, I declare three methods in our AIDL interface as add(), getPid() and getUid().

You can download the full source code here


Popular posts from this blog

Open Gallery and get the selected image in appplication

Dismiss or Cancel dialog by swipe gesture

AlarmManager and Notification in Android