Android Binder Inter-Process Communication Mechanism
To implement a Binder-based service, first define an interface using AIDL. For example, create IMyService.aidl:
package com.example;
interface IMyService {
int calculateSum(int first, int second);
}
The Android build system automatically generates a Java stub class from this AIDL file, which includes both the client-side proxy and server-side implementation skeleton.
Next, implement the service on the server side by extending Service and returning a concrete implementation of the generated Stub class:
package com.example;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
public class CalculatorService extends Service {
private final IMyService.Stub serviceBinder = new IMyService.Stub() {
@Override
public int calculateSum(int first, int second) throws RemoteException {
return first + second;
}
};
@Override
public IBinder onBind(Intent intent) {
return serviceBinder;
}
}
The Stub class implements the remote interface and handles marshaling/unmarshaling of parameters across process boundaries. It runs in the service’s process and receives calls from clients via the Binder driver.
On the client side, bind to the service and obtain a reference to the remote interface using asInterface():
package com.example.client;
import android.content.ComponentName;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.IMyService;
public class ClientActivity extends AppCompatActivity {
private IMyService remoteCalculator;
private final ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
remoteCalculator = IMyService.Stub.asInterface(binder);
try {
int sum = remoteCalculator.calculateSum(17, 23);
Toast.makeText(ClientActivity.this, "Result: " + sum, Toast.LENGTH_SHORT).show();
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
remoteCalculator = null;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindService(new Intent(this, CalculatorService.class), connection, BIND_AUTO_CREATE);
}
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(connection);
}
}
The client holds a reference to a Proxy object (created internally by asInterface()), which serializes method calls into Binder transactions. These transactions are sent to the server process, where the corresponding Stub method is invoked. The result is then marshaled back to the client.
This architecture abstracts the complexity of cross-process communication: developers interact with a local interface, while the Binder system handles serialization, thread dispatching, and security permisions transparently.