Android aidl-Implementation Mechanism Analysis

Source: Internet
Author: User
Tags mremote

1. based on the previously written aidl usage, we are going to study the activitymanager framework during this time and conduct more in-depth research on aidl. Because the android framework uses a large number of process communication mechanisms, it is necessary to carefully study the aidl implementation mechanism before studying the android framework.

2. As mentioned earlier, aidl is the abbreviation of Android interface definition language. It is a description of a process communication interface. It is compiled by the SDK interpreter and compiled into JavaCodeUnder the gen directory, the class path is the same as that of the aidl file.

3. aidl Interface
Package com. Cao. Android. demos. Binder. aidl;
Import com. Cao. Android. demos. Binder. aidl. aidlactivity;
Interface aidlservice {
Void registertestcall (aidlactivity CB );
Void invokcallback ();
}

 

Aidlservice. Java describes in detail the implementation of the aidl interface. As shown in the figure above, aidlactivity. aidl is compiled into an interface aidlactivity, a stub class stub, and a proxy class proxy
Public interface aidlservice extends Android. OS. iinterface // Java interface implementation corresponding to the interface defined in aidlactivity. aidl
Public static abstract class stub extends Android. OS. Binder implements com. Cao. Android. demos. Binder. aidl. aidlservice
// Inherit from Android. OS. Binder, receive Communication Data in ontransact, call the aidlservice interface method through different communication parameter codes, and write back the call result aidlservice interface method.
// Server implementation
Private Static class proxy implements com. Cao. Android. demos. Binder. aidl. aidlservice
// Implement the aidlservice interface method, but the method only executes the proxy Remote Call operation. The specific method operation is implemented in the remote stub class.

 

In general, aidlactivity. aidl will generate an aidlactivity interface, a stub image class, and a proxy class. In fact, the idea of compiling and generating the WSDL file of the root axis is the same,
The stub image class must be implemented on the server. The proxy class is used by the client. Through the encapsulation of stub and proxy, the details of Process Communication are shielded. For the user, it is just a call to the aidlactivity interface.

 

4. Use aidl to check the implementation code of the aidlservice call based on the above ideas.
-- 1. Implement the aidlservice. Stub abstract class on the server, and return the implementation class in the onbind method on the server.
-- 2. When the client binds a service, serviceconnection. onserviceconnected gets the ibinder object returned by onbind.
Private serviceconnection mconnection = new serviceconnection (){
Public void onserviceconnected (componentname classname, ibinder Service ){
Log ("Connect service ");
Mservice = aidlservice. stub. asinterface (service );
Try {
Mservice. registertestcall (mcallback );
} Catch (RemoteException e ){

}
}
Note that mconnection serves as the call parameter in bindservice: bindservice (intent, mconnection, context. bind_auto_create );
-- 3. aidlservice. stub. asinterface (service );
Public static com. Cao. Android. demos. Binder. aidl. aidlservice asinterface (Android. OS. ibinder OBJ)
{
If (OBJ = NULL )){
Return NULL;
}
Android. OS. iinterface Iin = (Android. OS. iinterface) obj. querylocalinterface (descriptor );
// If bindservice is bound to the service of the same process, and the returned result is the stub object of the server, the stub object is directly operated on the client without Process Communication.
If (IIN! = NULL) & (IIN instanceof com. Cao. Android. demos. Binder. aidl. aidlservice ))){
Return (COM. Cao. Android. demos. Binder. aidl. aidlservice) iIn );
}
// Bindservice is bound to a service that is not bound to the same process. The proxy object is returned. OBJ = Android. OS. binderproxy object is packaged into an aidlservice. stub. Proxy proxy object.
// However, aidlservice. stub. Proxy can communicate with each other through Android. OS. binderproxy.
Return new COM. Cao. Android. demos. Binder. aidl. aidlservice. stub. Proxy (OBJ );
}
-- 4. Call the aidlservice interface method. If it is the same process, aidlservice is the service's Stub object. It is equivalent to directly calling the aidlservice interface method implemented by the stub object.
If it is a proxy object, it is called between processes. Let's look at a client call example:
Public void onclick (view v ){
Log ("aidltestactivity. btncallback ");
Try {
Mservice. invokcallback ();
} Catch (RemoteException e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
-- Mservice. invokcallback () is equivalent to calling proxy. invokcallback, which is called between processes. We can see the implementation of the proxy method.
Public void invokcallback () throws Android. OS. RemoteException
{
// Construct a parcel object that can be transmitted between processes
Android. OS. Parcel _ DATA = Android. OS. Parcel. Obtain ();
Android. OS. Parcel _ reply = Android. OS. Parcel. Obtain ();
Try {
// Descriptor = "com. Cao. Android. demos. Binder. aidl. aidlservice", which describes the stub object to call.
_ Data. writeinterfacetoken (descriptor );
// Stub. transaction_invokcallback identifies which interface method to call in stub. mremote is constructing the proxy object's parameter OBJ, that is, public void onserviceconnected (componentname classname, ibinder Service)
// The service parameter in. It is a binderproxy object that is used to transmit inter-process data.
Mremote. transact (stub. transaction_invokcallback, _ data, _ reply, 0 );
_ Reply. readexception ();
}
Finally {
_ Reply. Recycle ();
_ Data. Recycle ();
}
}
-- 5. binderproxy. transact localized implementation of this method
Public native Boolean transact (INT code, parcel data, parcel reply,
Int flags) throws RemoteException;
// The implemented local code/frameworks/base/CORE/JNI/android_util_binder.cpp-> static jboolean android_ OS _binderproxy_transact (jnienv * ENV, jobject OBJ,
Jint code, jobject dataobj,
Jobject replyobj, jint flags)
// How to implement specific process Communication in the C code, and further study later.
-- 6. receive data from the server process
-- Call Stack
# Aidlservice. stub. ontransact
Zookeeper aidlservice.stub(bindercmd.exe ctransact
# Nativestart. Run
-- Aidlservice. stub. ontransact
Public Boolean ontransact (INT code, Android. OS. Parcel data, Android. OS. Parcel reply, int flags) throws Android. OS. RemoteException
{
Switch (CODE)
{
Case interface_transaction:
{
Reply. writestring (descriptor );
Return true;
}
Case transaction_registertestcall:
{
Data. enforceinterface (descriptor );
Com. Cao. Android. demos. Binder. aidl. aidlactivity _ arg0;
_ Arg0 = com. Cao. Android. demos. Binder. aidl. aidlactivity. stub. asinterface (data. readstrongbinder ());
This. registertestcall (_ arg0 );
Reply. writenoexception ();
Return true;
}
// Transaction_invokcallback is determined by the parameters of the transact method called by the client. Code = transaction_invokcallback: Run
// Invokcallback method, which is implemented by the server stub class inherited from stud.
Case transaction_invokcallback:
{
Data. enforceinterface (descriptor );
This. invokcallback ();
Reply. writenoexception ();
Return true;
}

5. I have not studied how to call the local C code. With the later development of the android framework, I will send a blog to further explain how the C code at the bottom implements process communication, for more information about aidl Process Communication.

 

Original article: http://my.oschina.net/u/213924/blog/37310

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.