AIDL interprocess communication

package app.mabel.com.mylib;

interface ISimp {

int add(int a,int b);

int sub(int a,int b);

}

package app.mabel.com.mylib;

public interface ISimp extends android.os.IInterface {

public static abstract class Stub extends android.os.Binder implements app.mabel.com.mylib.ISimp {

private static class Proxy implements app.mabel.com.mylib.ISimp {

}

}

public int add(int a, int b) throws android.os.RemoteException;

public int sub(int a, int b) throws android.os.RemoteException;

}

Writing the server application

package app.mabel.com.testservice;

import android.os.RemoteException;

import app.mabel.com.mylib.ISimp;

public class SimpServiceImp extends ISimp.Stub {

@Override

public int add(int a, int b) throws RemoteException {

return a+b;

}

@Override

public int sub(int a, int b) throws RemoteException {

return a-b;

}

}

package app.mabel.com.testservice;

import android.app.Service;

import android.content.Intent;

import android.os.IBinder;

import android.support.annotation.Nullable;

public class MySimpService extends Service {

@Nullable

@Override

public IBinder onBind(Intent intent) {

return new SimpServiceImp();

}

}

<manifest xmlns:android=”http://schemas.android.com/apk/res/android"

package=”app.mabel.com.testservice”>

<application

android:allowBackup=”true”

android:icon=”@mipmap/ic_launcher”

android:label=”@string/app_name”

android:roundIcon=”@mipmap/ic_launcher_round”

android:supportsRtl=”true”

android:theme=”@style/AppTheme” >

<service android:name=”.MySimpService”>

<intent-filter>

<action android:name=”app.mabel.com.testservice.MySimpService”/>

</intent-filter>

</service>

</application>

</manifest>

Writing the Client application

public class MainActivity extends AppCompatActivity implements ServiceConnection{

@Override

public void onServiceConnected(ComponentName componentName, IBinder iBinder) {

}

@Override

public void onServiceDisconnected(ComponentName componentName) {

}

}

protected void onStart() {

super.onStart();

bindService(new Intent(“app.mabel.com.testservice.MySimpService”).setPackage(“app.mabel.com.testservice”)

,this,BIND_AUTO_CREATE);

}

private ISimp service;

@Override

public void onServiceConnected(ComponentName componentName, IBinder iBinder) {

service = ISimp.Stub.asInterface(iBinder);

}

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button btn=(Button)findViewById(R.id.btn1);

btn.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

try {

Log.d(“tag”,”res:”+service.add(10,20));

} catch (RemoteException e) {

e.printStackTrace();

}

}

});

}

How does it work

public int add(int a, int b) throws android.os.RemoteException {

android.os.Parcel _data = android.os.Parcel.obtain();

android.os.Parcel _reply = android.os.Parcel.obtain();

int _result;

try {

_data.writeInterfaceToken(DESCRIPTOR);

_data.writeInt(a);

_data.writeInt(b);

mRemote.transact(Stub.TRANSACTION_add, _data, _reply, 0);

_reply.readException();

_result = _reply.readInt();

} finally {

_reply.recycle();

_data.recycle();

}

return _result;

}

public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException {

switch (code) {

case INTERFACE_TRANSACTION: {

reply.writeString(DESCRIPTOR);

return true;

}

case TRANSACTION_add: {

data.enforceInterface(DESCRIPTOR);

int _arg0;

_arg0 = data.readInt();

int _arg1;

_arg1 = data.readInt();

int _result = this.add(_arg0, _arg1);

reply.writeNoException();

reply.writeInt(_result);

return true;

}

}

Extending the interface

in, out, inout keywords

interface ISimp {

int add(int a,int b);

int sub(int a,int b);

int addrr(in int[] arr);

}

@Override

public int addrr(int[] arr) throws RemoteException {

int sum=0;

for(int i=0;i<arr.length;i++)

sum+=arr[i];

return sum;

}

interface ISimp {

int add(int a,int b);

int sub(int a,int b);

int addrr(in int[] arr);

int fillArray(out int[] arr)

int convertToGray(inout int[] rgb)

}

Creating custom types

package app.mabel.com.mylib;

import android.os.Parcel;

import android.os.Parcelable;

public class CustomType implements Parcelable {

int num1;

String s1;

public int getNum1() {

return num1;

}

public void setNum1(int num1) {

this.num1 = num1;

}

public String getS1() {

return s1;

}

public void setS1(String s1) {

this.s1 = s1;

}

@Override

public int describeContents() {

return 0;

}

@Override

public void writeToParcel(Parcel parcel, int i) {

parcel.writeInt(num1);

parcel.writeString(s1);

}

public static final Parcelable.Creator<CustomType> CREATOR = new Parcelable.Creator<CustomType>(){

@Override

public CustomType createFromParcel(Parcel parcel) {

CustomType res=new CustomType();

res.num1 = parcel.readInt();

res.s1 = parcel.readString();

return res;

}

@Override

public CustomType[] newArray(int size) {

return new CustomType[size];

}

};

}

package app.mabel.com.mylib;

parcelable CustomType;

package app.mabel.com.mylib;

import app.mabel.com.mylib.CustomType;

// Declare any non-default types here with import statements

interface ISimp {

int add(int a,int b);

int sub(int a,int b);

int addrr(in int[] arr);

String getCustomName(in CustomType ct);

}

Asynchronous Service

package app.mabel.com.mylib;

import app.mabel.com.mylib.IAsyncListener;

oneway interface IAsync {

void calcSum(in int []arr, in IAsyncListener lis);

}

package app.mabel.com.mylib;

// Declare any non-default types here with import statements

oneway interface IAsyncListener {

void OnResponse(int res);

}

public class IAsyncImp extends IAsync.Stub {

@Override

public void calcSum(int[] arr, IAsyncListener lis) throws RemoteException {

int res=0;

for(int i=0;i<arr.length;i++)

res+=arr[i];

lis.OnResponse(res);

}

}

Button btn2=(Button)findViewById(R.id.btn2);

btn2.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

try {

asyncService.calcSum(new int[]{2, 3, 4, 5}, new IAsyncListener.Stub() {

@Override

public void OnResponse(int res) throws RemoteException {

Log.d(“tag”,”resp:”+res);

}

});

} catch (RemoteException e) {

e.printStackTrace();

}

}

});

A developer is responsible for developing a framework and system applications for devices powered by the Android operating system.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store