Binder works on the Linux layer, is a driver that does not require hardware, from a threading perspective, the binder code runs in the kernel state, the client program uses binder is done through the system call.
Binder is a monolithic architecture that includes server-side interfaces, binder drivers, and client interface three modules.
1. Service-side
A binder server is actually an object of a binder class that, once created, launches a hidden thread inside. The thread receives the message sent by the binder driver, receives the message, executes the ontransact () function into the Binder object, and executes the different service code according to the function's parameters.
Therefore, to implement a binder service, you must overload the Ontransact () method. The main task of the Ontransact () function is to convert the parameters of the Ontransact function into the parameters of the service function. The source of the Ontransact () parameter is entered when the client calls the Transact () function, so if Transact () has a fixed-format input, then Ontransact () has a fixed-format output.
2. Binder Driver
Binder driver is a virtual device/dev/binder that is specifically set up by Android in the kernel to complete interprocess communication. When any of the server binder objects are created, a Mremote object is created in the binder drive, and the type of the object is also the Binder class. When a client drug accesses a remote service, it passes through the Mremote object. The Mremote object also overloads the Transact () method, and the overloaded content mainly includes:
- In the pattern of message communication between threads, send the parameters passed by the client to the server.
- Suspends the current thread, which is the client send thread and waits for the service thread to be notified after the specified service function has been executed.
- Receives notification of the service-side program, then resumes execution of the client thread and returns to the client code area.
3. Client (application side)
To access the remote service, the client must obtain a mremote reference to the remote service in the Binder object. Once the client has obtained mremote, it can call its Transact method directly.
4. Binder Examples
(1) Service-side implementation
As long as you create a new service class based on the Binder class, then overload the Ontransace () function.
public class Musicplayerservice extends Binder {
protected Boolean ontransact (int code, PARCEL data, Parcel reply, int flags) {
Switch (code) {//code is used to identify which function the client expects to invoke on the service side, as agreed by both parties. This value is consistent with the value of the first parameter in the client's Transact () function (which can be implemented through the Aidl tool in the SDK).
Case PLAY: {
Data.enforceinterface ("Musicplayerservice"); For some kind of validation, it corresponds to the client's Writeinterfacetoken ().
String FilePath = data.readstring (); read out the parameters passed from the client. The position of the parameter in the data variable needs to be agreed between the client and the server.
Start (FilePath);
}
Break
Default
Break
}
}
public void Start (String filePath) {
}
public void Stop () {
}
}
(2) Client implementation
Services are divided into system services and client services. A system service is a service that can be obtained through Getsystemservice (), a client service that refers to a custom service provided by an application.
How do clients get binder objects?
AMS provides the StartService () function to start the client service. For clients, you can use the following two functions to establish a connection to a service:
i) public componentname startservice (Intent Intent);
This function is used to start the service specified by intent, and after the service has started, the client does not yet have a binder reference to the server, so it is not possible to invoke the functionality of any service at this time.
II) Public boolean bindservice (Intent Service, serviceconnection conn, int flags);
The function is used to bind a service, and the second parameter is the interface class, which is defined as:
Public interface Serviceconnection {
public void onserviceconnected (componentname name, IBinder service);
public void onservicedisconnected (componentname name);
}
When a client requests AMS to start a service, if the service starts normally, AMS calls the Applicationthread object in the Activitythread class remotely, and the calling parameter contains a binder reference to the service , and then callback the Conn interface of Bindservice in Applicationthread.
As a result, the client can save the parameter service as a global variable in the Onserviceconnected method, so that the remote service can be invoked anywhere on the client.
Binder FRAME (1)