Binder is used to complete interprocess communication (IPC), that is, multiple processes are "not" together, from the thread point of view,thebinder driver code runs in the kernel state, the client program calls binders are done through system calls. Binder is an architecture that provides a server-side interface,binder driver, and client interface three modules.
The main content of the overloaded ontransacto function is to convert the parameters of the ontmnsact () function to the parameters of the service function, while the ontransact () the parameter source of the function is the client call Transact () function is entered.
When any of the service-side Binder objects are created, the binder driver creates a
The Mremote object, which is also the Binder class type. When a client accesses a remote service, it passes through the mremote object. The client appears to be directly invoking the binder for the remote service , while in fact it is brokered through the binder driver. There are two binder objects, one is the binder object on the service side , and the other is the binder driver. Binder object, the difference is that the objects in the binder drive no longer produce a single thread.
Service-Side design
public class Musicplayservice extends binder{/*** Musicplayservicemusicplayservice=newmusicplayservice () opens a new thread * Code: Is the client and the server contract, the number that represents the variable access (multiple variables common one number) * Data: The data of the client is stored * Replay: The server returns the datastore * FLAG:IPC Call mode * One is bidirectional, with constant 0, It means that the server will return certain data after execution of the specified service; * One is unidirectional, expressed in constant 1, meaning that no data is returned. */@OverrideProtected Boolean ontransact (int code,parcel data,parcel reply,int flags) throws Remoteexception{switch ( Code) {case1000://for some kind of verification, it corresponds to the client's Writeinterfacetoken () data.enforceinterface ("Musicplayservice"); String arg=data.readstring (); String arg1 = data.readstring (); Break;case2000:data.enforceinterface ("Musicplayservice"); arg=data.readstring (); break;} Returnsuper.ontransact (code,data,reply,flags);} Publicvoidstart () {}publicvoidstop () {}}
Binder Drive
When a Binder object is created, a Binder object is created inside the server process , and A binder driver creates a Binder object. If you get the binder from a remote Server, only the Binder object in the Binder driver isreturned . If you get binder objects from within a server-side process , you get Binder objects for the server itself .
Client Design
Publicclass musicplayclient{
/**
* Two questions:
* 1 . How does the client get the Binder Object reference on the server ?
* Why use Binder? in order to provide a global service, any application of the system can be used, the solution is to use service services
* 2, the client and the server must be agreed in advance two things : The service side function parameters in the order of the package, the service side of the different functions of int Type identification
*
*service is how to solve the above two problems
* First question:
* clients Start Services through binderservice and startservice
* Bindservice (service,conn,flags) and startservice (service)
* serviceconnection{
* onserviceconnected (componentnamename,ibinderservice);
* }
* Bind to bind the service if service started successfully, activitymanagerservice activitythread applicationthread service binder reference, and then applicationthread callback bindservice conn interface, the client can set it as a global variable.
* second question:
* The aidl tool is available in androidsdk to convert a aidl file into a Java class that overloads the transact and ontransact methods that unify the parameters of parcel storage and parcel reading
* Proxy: Client Access Server Agent, uniform package parameter write order
* stub: Used by the service side
* When a Binder object is created, a Binder object is created inside the server process , and A binder driver creates a Binder object. If you get the binder from a remote Server, only the Binder object in the Binder driver isreturned . If you get binder objects from within a server-side process , you get the binder on the server itself
*/
Public void sendpacket ()throwsremoteexception{
Ibinde rmremote=null;
String arg="Agr0";
Parcel Data=parcel.obtain ();
Parcel Reply=parcel.obtain ();
Data.writeinterfacetoken ("Musicplayservice"); // Callout remote service name
Data.writestring (ARG);
Int flags=0;
/**
* When the client thread enters the binder driver,thebinder driver suspends the current thread and sends a message to the remote service.
* The message contains the package that the client sent in. After the server has taken the parcel, the package is disassembled and the specified service function is executed .
* after execution, the execution results are placed in the reply package provided by the client . The server then sends a notify message to the Binder driver,
* This allows the client thread to return from the Binder Drive code area to the client code area.
*/
Mremote.transact (1,data,reply,flags);
Ibinderbinder=reply.readstrongbinder ();
Data.recycle ();
Reply.recycle ();
}
}
Android Binder Tutorial