Detailed explanation of Aidl use of Android interprocess communication

1、 Overview

Aidl means Android interface finalization language, which translates into Android interface definition language. It is a description language used to define the communication interface between server and client, which can be used to generate code for IPC. In a sense, Aidl is actually a template, because in the process of use, what actually works is not the Aidl file, but an iiinterface instance code generated based on it. Aidl is actually a template to avoid repeated code writing

The purpose of designing Aidl language is to realize interprocess communication. In Android system, each process runs in an independent memory, which completes its own activities and is separated from other processes. However, sometimes we have the need for interaction between applications, such as data transmission or task delegation. Aidl was born to meet this need. Through Aidl, you can get the data of another process in one process and call its exposed methods, so as to meet the needs of inter process communication

Generally, the application that exposes the method to other applications for calling is called the server, and the application that calls the method of other applications is called the client. The client interacts by binding the service of the server

2、 Grammar

The syntax of Aidl is very simple and basically consistent with the Java language. The rules to remember are as follows:

1. Aidl files take. Aidl as the suffix

2. The data types supported by Aidl are as follows:

3. Aidl files can be divided into two categories. A class is used to declare data types that implement the Parcelable interface for other Aidl files to use those data types that are not supported by default. Another class is used to define interface methods and declare which interfaces to expose for client calls. Directional tag is used to mark the parameter values of these methods

4. Directional tag. Directional tag indicates the flow direction of data in cross process communication. It is used to mark the parameter values of methods. It is divided into in, out and inout. Where in means that data can only flow from the client to the server, out means that data can only flow from the server to the client, and inout means that data can flow in both directions between the server and the client. In addition, if the parameter value type of Aidl method interface is: basic data type, string, charsequence or other method interfaces defined in Aidl file, the directional tag of these parameter values is and can only be in by default. Therefore, in addition to these types, other parameter values should clearly indicate which directional tag to use. Specific differences in the use of directional tags will be described later

5. Define the guide package. The package name of the referenced data type needs to be clearly indicated in the Aidl file, even if the two files are under the same package name

3、 Server code

Here is an example for demonstration. The function to be realized is that the client calls the method of the server by binding the service of the server, obtains the book list of the server, adds books to it, and realizes data sharing between applications

The first is the server code

Create a new project, and the package name is defined as com.czy.server

First of all, a Book class needs to be used in the application, and the book class needs to be used between the two applications, so the book class also needs to be declared in the Aidl file. In order to avoid the error that the file cannot be created due to duplicate class names, the book Aidl file needs to be created first, and then the book class needs to be created

Right click to create an Aidl file named book

After creation, the system will create an Aidl folder by default. The directory structure under the folder is the package name of the project, and the book.aidi file is in it

There will be a default method in the book.aidl file that can be deleted

At this point, you can define the book class, which contains only one name attribute and implements the Parcelable interface

Now modify the book. Aidl file to an Aidl file that declares the Parcelable data type

In addition, according to the initial assumption, the server needs to expose to the client a method to obtain the book list and a method to add books. These two methods must first be defined in the Aidl file and named bookcontroller.aidl. Note that the guide package needs to be specified here

As mentioned above, what really works in interprocess communication is not the Aidl file, but the file generated by the system. You can view the file generated by the system in the following directory. After that, you need to use the internal static abstract class stub

After creating or modifying Aidl files, we need to clean the project to make the system generate the files we need in time

Now you need to create a service for the client to bind remotely, which is named aidlservice here

You can see that the onbind method returns the bookcontroller. Stub object, which implements the two methods defined in it

Finally, there is another point to note on the server. Because the service on the server needs to be bound remotely by the client, the client can find the service by specifying the package name first, then configuring the action value, or directly specifying the service class name

If you bind the service by specifying the action value, you also need to change the service declaration as follows:

This example adopts the scheme of configuring action

4、 Client coding

The client needs to create a new project. The package name is com.czy.client

First, you need to copy the Aidl file and the book class from the server, and copy the entire Aidl folder to the same level as the Java folder without changing any code

After that, you need to create the same package name as the server-side book class to store the book class

Modify the layout file and add two buttons

The two buttons are respectively used to obtain the list of books on the server and add books. When adding books, the server also changes the name attribute of the book object to observe the changes of data on the client and server

First, click to obtain the book list, and the data is obtained without error

Then click the button to add a book. You can see that the data modification of the server is synchronized to the client at the same time

So far, the communication between the client and the server has been realized. The client obtains the data of the server and transmits the data to the server

5、 Directional tag

Finally, let me talk about the differences between the three directional tags. The inout type is used above, and the changes of the server to the data are synchronized to the client at the same time. Therefore, it can be said that the data flows in both directions

The expression of in type is that data can only be transmitted from the client to the server, and the modification of data by the server will not affect the client

The expression of out type is: data can only be transmitted from the server to the server. Even if the client passes an object to the method interface, the attribute value in the object is empty, that is, it does not contain any data. After the server obtains the object, any operations on the object will be synchronized to the client

Here's another practical demonstration

First modify the bookcontroller.aidl file on the server side and add two new methods to it

The bookcontroller.stub object of aidlservice class needs to be modified as follows:

Synchronously modify the bookcontroller.aidl file of the client

Add two more buttons to the layout file, which are respectively used to add data of different directional tags

In addition, there is another place that needs to be modified, that is, the book class. Because the out type does enable the client to pass an object without any data back to the server, but the object is not directly equal to null, it indicates that the system still needs to instantiate the book class, but the current book class only has a parametric constructor, so it is also necessary to modify the book class and add a parameterless constructor for the system to use

Click the three buttons respectively to see that when the server obtains the book object transmitted by the client in the out mode, it does not contain the attribute value of book name

That's all about Aidl. The above code download is also provided here. For convenience, I save the client code as a compressed file and put it under the server project file

Android Aidl usage details

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>