Android's Aidl

Source: Internet
Author: User

I. Significance of AIDL:

Aidl full name is Android Interface definition Language, which is the Android interface definition language. Aidl is a template that appears to prevent us from writing some stereotyped code over and over again.

Objective: To achieve inter-process communication, and to communicate between processes in the case of multi-process concurrency.

The difference from Messenger: Messenger is a serial way to handle the message sent by the client, if there are a large number of concurrent requests, then Messenger is not suitable for these situations, and the role of Messenger is mainly to deliver messages, Many times we may need to invoke the service-side approach across processes, a situation that cannot be done with Messenger, but Aidl can. Aidl is also the bottom-level implementation of Messenger, so the nature of Messenger is also aidl, but the system has made it easy for us to encapsulate the upper layer of the call.

Two. Aidl's syntax:

(1) file type: The suffix of the file written with Aidl is. Aidl, not. java.

(2) Data type: Aidl Some data types are supported by default, they are not required when using these data types, but except for data types other than those types, they must be guided before use, even if the target file is under the same package as the. aidl file currently being written-in Java, This situation does not require a guide package. For example, now that we've written two files, one called Book.java, the other called Bookmanager.aidl, they're all under the Com.lypeer.aidldemo package, and now we need to use the book object in the. aidl file, then we have to. Aidl file is written in import Com.lypeer.aidldemo.Book; Even the. java files and. aidl files are under one package.

 · basic data type (Int,long,char,boolean,double,boolean,short,byte)

                  · string types and charsequence type;

· List type: Only support ArrayList, each element inside must be able to be aidl support, List supports generics;

· Map type: Only support HashMap, each element inside must be able to be supported by AIDL, including key and Value,map do not support generics.

· Parcelable: All matters parcelable The object of the interface;

· Aidl: All Aidl interfaces themselves can also be used in aidl files.

Two types of aidl files: All aidl files can be broadly divided into two categories. A class is used to define a Parcelable object for use by other aidl files with data types that are supported by default in Aidl. A class is used to define a method interface for use by the system to complete cross-process communication. As you can see, both types of files are "defined" and do not involve specific implementations, which is why it is called "Android Interface Definition language".

As an example:

Example 1:

BOOK.AIDL//////////////////////////////     for the first class of aidl files   For use with other aidl files // Note: Book.aidl and Book.java's package name should be the  same com.ryg.chapter_2. Aidl;

// Note that parcelable is lowercase

Example 2:

//Ibookmanager.aidl
Example of the second type of aidl file
PackageCom.ryg.chapter_2.aidl; //Import packages for non-default support data types that you need to use ImportCom.ryg.chapter_2.aidl.book;Interfaceibookmanager{//No need to add anything before all the return values, no matter what data typeList<book>getbooklist (); //In addition to Java basic types and types other than String,charsequence, when you pass a parameter//need to be preceded by a directional tag, the specific amount of what needs to be determined voidAddbook (in Book book);}

object classes to implement the Parcelable interface:

refer to this bolg:http://blog.csdn.net/luoyanglizi/article/details/51980630

The data that will be transferred into a form that can flow between memory.  This process of transformation is called serialization and deserialization.

For example, now we are going to upload an object's data from the client to the server, and we can serialize the object on the client, convert the data contained in it into a serialized stream, then transfer the serialized stream to the server's memory, and then deserialize the data stream on the server, To restore the data contained therein-in this way, we achieve the purpose of accessing data from another process in one process.

Aidl in the In,out,inout:

http://blog.csdn.net/luoyanglizi/article/details/51958091

The directional tag in aidl represents the flow of data in cross-process communication, where in indicates that the data can only be flowed to the server by the client, and out indicates that the data can flow only from the server to the client, while inout indicates that the data can flow in two directions between the server and the client. Where the data flow is directed at the object of the incoming method in the client. In the direction of the tag, the expression is that the server will receive a complete data of that object, but the client's object will not be changed by the service side of the changes to the parameters of the server, the expression is that the server will receive the object's parameter is empty object, However, the client will synchronize the changes after any modification to the null object received by the server, and the server will receive the complete information from the client when the InOut is directed to the tag, and the client will synchronize any changes to the object from the server.

because the object of the default generated template class only supports directional tag for in. for what? Because the default generated class has only the Writetoparcel () method, you need to implement the Readfromparcel () method if you want to support a directed tag for out or inout--and this method is not actually Parcelable interface inside, so we need to write from scratch.

Readfromparcel () Method:

After adding the readfromparcel () method, the object of our book class can be used out or inout as its directional tag in the Aidl file.

Three. Quickly generate Aidl files in Androidstudio

In Eclipse

Four. PostScript

Error encountered during the process:

(1) Java.lang.SecurityException:Binder invocation to an incorrect interface error problem

Workaround: The client's AIDL package name should be the same as the service-side package name

(2) Error: The system cannot find the Book.java (object) file

Workaround One:

    • Modify the Build.gradle file: Add the following to the middle of the android{}:
    • Sourcesets {    main {        = [' Src/main/java ', ' Src/main/aidl '    }}

Workaround 2:

Put the Java file into the Java package: Put the Book.java in the Java package under any package, keep its package name unchanged, and book.aidl consistent. As long as its package name does not change, book.aidl can find Book.java, and as long as Book.java in the Java package, then the system can find it.

Example:

run both the client and the server to the same virtual machine

The last successful Logcat

Client

Service side

aidl Example Demo Download Portal: Https://github.com/Qinxiaoshou/AIDLDemo

Android's Aidl

Related Keywords:

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.