12.4 Android Remote Methods and AIDL

Hello readers!!! Hope you are enjoying our tutorials and learning a new technology. Today we shall talk about remote methods and AIDL. It is the most renowned procedure to implement interprocess communication. We have already studied other ways for implementing interprocess communication but there is always a better and effective solution of every problem. It is theoretical but we must know about this. So let us not waste time and start studying this new section.

12.4.1 Introduction

As the name suggests remote methods are the method calls which facilitates how one program can provide other programs with access to its methods. Android APIs make use this procedure to enhance its capability. For example TelephonyManager uses remote object interface to share and manage the phone hardware in android. We have certain methodology to implement and create remote methods in Android framework and they are as follows:

  1. First step is to define interface in AIDL
  2. Second step is to write methods which match the signatures in interface that performs the operation we wanted to do in our program. In other words to implement the interface.
  3. Last step is to invoke the calls where we want use them.

We use Android Interface Definition language (AIDL) is used for this purpose. So AIDL facilitates interprocess communication between services and other application components. These components may include components running within different applications or within separate processes. This in turn provided our services the power to support multiple applications across process boundaries. When we talk about passing objects from one process to another we need to deconstruct these objects in OS primitive level so that operating system can marshal objects across boundaries of applications. So they act as parcels hence we need to implement them as parcelables.

12.4.2 AIDL

AIDL simplifies the code so that processes can exchange the objects. This interface lets us create public methods within services which can accept and returns object parameters and return values between processes. For communication between processes the data which is stored in memory need to be moved across process boundaries. Data has to be packaged for transport i.e. marshaled and again put into right member variables after data has been moved across the process boundary i.e. unmarshalled.  Basic data types of Java are easy to marshal such as String but complex data types such as multi-dimensional arrays are difficult to marshal as one object can hold references to other objects which requires following every reference. Hence we need to marshal all data that it references. Thus marshalling and unmarshalling need to be performed on parameters in a remote method call, It lets us pass data from one application to another and return results.

It is sometimes hard to understand code that had to carry out the job every place where it uses interprocess communication. Thus most implementations of remote objects or components use an interface definition language that generates calls to marshalling methods. Syntax of interface definition language resembles the main language in use so that remote procedure call appropriately resembles a normal method call. But please do not confuse interface definition language is actually a separate language.

Syntax of AIDL is similar to the Java interface definition language. In AIDL we label the parameters as in, out or inout and they work in similar way:

  • In: The parameter which is labeled as in are transferred to the remote method
  • Out: The parameter which is labeled as out are returned to the caller from remote method.
  • Inout: This parameter will transfer data to the remote method and refer to a value transferred from the remote method when it returns.

Android Eclipse plugin compiles any AIDL file saved in Eclipse. Both the calling and implementing side of a remote method interface share the information in the AIDL file. AIDL needs to create code that moves the parameters between applications. AIDL supports the following data types and they are as follows:

  • Primitive data types of Java language (int, Boolean, float, char, etc.)
  • String and CharSequence values
  • List objects include generics where each element is a supported type
  • Map objects where every key and element is of a supported type
  • An import statement is always required for AIDL-generated interfaces
  • An import statement is always required for Classes that implement the Parcelable interface

Following section shows the way to define an interface to remote object:

interface InterfaceSecondary{
*Request PID of the service
Int getPid();
/** Let us consider integer and double primary data type

In order to make a class parcelable they must implement interface Parcelable. Consider the following example implementation.

package com.android.tution.exAIDL;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.location.Location;
import android.os.Parcel;
import android.os.Parcelable;
public class Sample implements Parcelable {
private Date date; private String details;
private Location location;
private double magnitude;
private String link;
public Date getDate() { return date; }
public String getDetails() { return details; }
public Location getLocation() { return location; }
public double getMagnitude() { return magnitude; }
public String getLink() { return link; }
public Sample (Date _d, String _det, Location _loc,
double _mag, String _link) {
date = _d;
details = _det;
location = _loc;
magnitude = _mag;
link = _link;
public String toString(){
SimpleDateFormat sdf = new SimpleDateFormat(“HH.mm”);
String dateString = sdf.format(date);
return dateString + “:” + magnitude + “ “ + details;
private Sample(Parcel in) {
details = in.readString();
magnitude = in.readDouble();
Location location = new Location(“generated”);
link = in.readString();
public void writeToParcel(Parcel out, int flags) {
public static final Parcelable.Creator<Sample> CREATOR =
new Parcelable.Creator<Sample>() {
public Sample createFromParcel(Parcel in) {
return new Sample(in);
public Sample[] newArray(int size) {
return new Sample[size];
 public int describeContents() {
return 0; } }

Figure example

Congratulations ladies and gentleman we have successfully accomplished the knowledge of remote methods and AIDL. See you in the next section with something new. Till then keep practicing. Happy App Developing!!!