Basic Introduction of Binder

Binder is mainly used for inter-process communication. A Binder communication is divided into a client and a server. Simply speaking, the client obtains the IBinder object of the server by binding the server. Then the client can send data to the server through the transact method through this proxy object, realizing the communication between processes.

Binder data transfer

The process by which Binder passes data is primarily memory mapping. Memory mapping is like the shortcuts on our computers, and the shortcuts we use are actually mapped to real files. With Binder, clients copy data into the Linux kernel with the transact method of a proxy object. Servers then retrieve data from that kernel using memory mapping, call it back in onTransact, process it and return it.

How to use AIDL

Server side: 1. Create interface, 2. Define binder, implement interface, 3.

Client: 1. Bind service, 2. Implement ServiceConnection binding listener, 3. On a successful binding callback, the IBinder is converted into an AIDL interface proxy object.

Once the client-server binding is successful, the object can be proxyed through AIDL’s interface as if it were a local method. Note that objects passed between AIDL implement the Parcelable interface.

AIDL binding process

The client invokes the bindService method to initiate a binding service request. Then, the ServiceManager obtains ActivityManagerService (AMS), and then initiates a bindService request to the server through AMS. The server then receives the binding request, sends a Message to the binding service as a Handler Message mechanism, processes the binding request in ActivityThread, calls onBind, and returns the corresponding IBinder object. The operation of returning the IBinder object is basically similar to the binding process via ServiceManager or AMS.

AIDL and Binder

The use of AIDL is essentially the encapsulation of Binder mechanism, which is mainly to encapsulate Binder as a proxy object. From the user’s point of view, it is as if the client directly calls the code of the server.