“This is the fifth day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
An overview,
AIDL stands for Android Interface Definition Language. AIDL is a description Language used to define the communication Interface between the server and the client, which can be used to generate code for IPC. AIDL is actually a template in a sense, because it’s not an AIDL file that actually works, it’s an instance of IInterface code that’s generated from it, and AIDL is a template that’s created to keep us from writing code again and again.
AIDL was designed to enable interprocess communication. In Android, each process runs in a separate block of memory where it performs its activities, separated from other processes. But sometimes we have a need to interact with each other, to compare and transfer data or delegate tasks, and AIDL was created to meet this need. With AIDL, you can retrieve data from one process and invoke exposed methods from another process to meet the needs of interprocess communication.
Generally, the application that exposes methods to call other applications is called the server, and the application that calls methods of other applications is called the client. The client interacts by binding the Service of the server.
Second, the grammar
The syntax of AIDL is very simple and basically the same as that of the Java language. Here are some rules to keep in mind:
-
AIDL files have the.aidl suffix
-
AIDL supports the following data types:
- Eight basic data types: byte, char, short, int, long, float, double, Boolean
- String, CharSequence
- Implements the data type of the Parcelable interface
- The List type. The data carried by the List must be of a type supported by AIDL, or another declared AIDL object
- The Map type. The data hosted by the Map must be of the type supported by AIDL or other declared AIDL objects
-
AIDL files fall into two categories. A class is used to declare data types that implement the Parcelable interface so that other AIDL files can use data types that are not supported by default. Another class defines interface methods, declaring which interfaces to expose for client calls. Directional tags are used to label the parameter values of these methods.
-
Orientation of the Tag. Directional Tag represents the flow direction of data in cross-process communication, and is used to mark the parameter values of methods, including in, OUT and InOUT. In indicates that data flows only from the client to the server, out indicates that data flows only from the server to the client, and inout indicates that data flows bidirectional between the server and the client. In addition, if the parameter value type of the AIDL method interface is: For basic data types, String, CharSequence, or other method interfaces defined in AIDL files, the default Tag for these parameter values is in, so all parameter values except these types need to be clearly marked with which Tag to use. Specific differences in the use of directional tags will be described later.
-
Clear guide package. You need to specify the package name of the referenced data type in an AIDL file, even if both files are under the same package name.
Server coding
Here is a practical example as a demonstration, the function to be realized is: the client invokes the method of the server by binding the Service of the server, obtains the book list of the server and adds books to it, and realizes data sharing between applications
First of all, we need to use a Book class in the application, and the Book class is used between the two applications, so we also need to declare the Book class in the AIDL file. To avoid the error of creating a file with duplicate class names, you need to create the Book AIDL file first and then the Book class
Right click on a new AIDL file and name it Book
By default, an aiDL folder is created. The directory structure in this folder is the name of the project package, and the book. aidi file is in it
There is a default method in the book.aidl file that can be deleted
Now you can define the Book class, which contains only a Name attribute and implements the Parcelable interface
public class Book implements Parcelable { private String name; public Book(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @override public String toString() {return "book name: "+ name; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(this.name); } public void readFromParcel(Parcel dest) { name = dest.readString(); } protected Book(Parcel in) { this.name = in.readString(); } public static final Creator<Book> CREATOR = new Creator<Book>() { @Override public Book createFromParcel(Parcel source) { return new Book(source); } @Override public Book[] newArray(int size) { return new Book[size]; }}; }Copy the code
Now modify the book.aidl file to declare the Parcelable data type aiDL file
package com.czy.server;
parcelable Book;
Copy the code
.
Android DIDL (2)