Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Android Binder Inter-Process Communication Mechanism

Tech May 18 3

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.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.