Android Basics-A beginner's aidl binder mechanism on the application layer

Source: Internet
Author: User
Tags stub

The binder mechanism of AIDL on the application layer for beginners to know

First you have to understand several concepts:

Ipc:inter-process communication, inter-process communication or cross-process communication. It is simple to understand that an application can have multiple processes, but the need for data exchange requires IPC, or data exchange between two applications.

Binder:binder is a class of Android that implements the IBinder interface. From an IPC perspective, Binder is a way of communicating across processes in Android. With this binder object, the client can obtain services or data provided by the server, and the services include General Service and AIDL based services.

Aidl:android Interface Definition language, which is a description language of the Android internal process communication interface.

First, the use of aidl

Service side:

Create a service-side project, in the project right-click New->aidl->aidl file, the default direct point to determine, this will appear in the project a aidl file:

We open this Aidl file and we create a method that we need to test:

Since Android Studio is to compile manually to generate the corresponding Aidl Java file, since the Aidl file is an interface , then there must be a class to implement this interface, click Compile, the system automatically generates a Java class, The code for this Java class is the principle of the entire binder mechanism (which is explained in the second step below):

since it's a server, we're going to start writing services , creating a class that inherits the service:

since it is a service , it must be configured in the manifests file:

Now that the server is written, open the emulator to start the program , remember to open the service in the code:

Client:

In the project right click New->module, by default determined, finish:


The key step is to copy the server aidl the entire folder (including the package inside, aidl file, intact) pasted into the client corresponding to place Aidl :


Do not forget that the client also compiles manually :

Okay, let's write the client code (we put a "aidl" button in Mainactivity, bind the service first, and then click the button Call):

Test results (Start the server, turn on the service, then turn on the client, bind the remote service):


Principle analysis of binder mechanism of aidl

Analysis principle:

Let's analyze This class.

First of all inherit iinterface, so he is also an interface, there must be a method in the interface, the code is positioned to the end there are 2 methods.

These two methods are basictypes and add, which is the 2 methods of our server.

It is then found that there are 1 internal class stubs in the interface, inherited from the Imyaidlinterface interface, and the code is anchored to the stub class.

This stub has a construction method, Asinterface, Asbinder, Ontransact (not introduced first).

It is then found that the internal class stub has an inner class, the code is positioned to the proxy (which we call the proxy) class, and is also inherited from itself (Imyaidlinterface) interface, so the two methods of implementing the interface.

In this class we will find that there are 2 identities: to distinguish two methods, in the end you remotely request which method's unique identity, the code is positioned to the end of the proxy class .

Looking back, remember what our client did? Answer: Bind a service, get an interface in callback method (Imyaidlinterface), it is the method that directly statically uses static class stub of imyaidlinterface inside Asinterface : (IELTS high score composition Good we go to trace to the stub class Asinterface this method)

the code navigates to the stub class Asinterface method :

Just make a few judgments and take a look at the last word : We're going to pass over the obj or pass it to its proxy class to handle, returning the object of the proxy class.

So in the client's imyaidlinterface= ..., is to get its proxy class, OK, this time to see the client call proxy class what to do :

He calls the Add method of the proxy class, and the code navigates to the Add method of the proxy class :

You will find that it writes the data into the _data, finally calls the Transact method , passes in the _data data, uniquely identifies the stub.transaction_add.

And then this Transact method is passed through the bottom, after the bottom end, where are these parameters sent? Answer: The underlying will go to the Ontransact method in the stub class by judging the unique identifier and determining the method:

In this place will pass over the parameters of the unpacking, Readint method. Then call the This.add method, this refers to the service side, call the service side of the Add method:

The results that will be obtained are written to reply:

Finally, the last word returns the ontransact method of the system, passing in the result reply:

So the result we get on top is reply (answer: 3):

The final summary of the entire process



iii. Some errors in Aidl writing

Error One:

This error is most likely the server you wrote, forgot to return the MYS, returned a null

Error Two:

This error is most likely the server is missing the exported= "true" attribute in the manifests file

Android Basics-A beginner's aidl binder mechanism on the application layer

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.