AIDL communication principle and androidaidl Principle
AIDL (Android Interface Definition Language) implements inter-process communication through defining communication interfaces. This is a tool provided by Google for communication between Android application processes. To understand the Communication Principles of AIDL, we need to solve the following two problems.
1. Why AIDL?
We all know that AIDL aims to communicate with each other. Therefore, before learning about the AIDL principle, you must first understand what is inter-process communication. Inter-process communication involves two terms: inter-process communication and inter-process communication. Inter-process communication is between two or more processes. Therefore, we need to clarify the concept that inter-process communication involves multiple entities. What is communication? Call, email, and QQ messages all communicate with each other. Data Exchange is what they share. To put it simply, inter-process communication refers to data exchange between multiple entities, and the data is run-time data, because the communication entity is generated at runtime. In general, how does our application exchange data? The simplest way is to share the memory, that is, to create A Memory Sharing area, and then process B writes to the Memory Sharing area. process A reads from the Memory Sharing area to complete communication. However, it does not work in Android, because a separate process runs in its own virtual machine and has its own memory ing. Therefore, Memory Sharing cannot be implemented, and it can only be achieved through AIDL.
2. How does AIDL work?
Before that, we will first discuss the AIDL implementation steps:
1) define the AIDL file;
2) Implement the interfaces defined in the AIDL file;
3) Expose interfaces;
4) call;
These four processes can be analogous to the c/s model. If a client needs to access a webpage on the server, how many steps does it need? It also requires four:
1) define communication rules. The existing rules are available: TCP/IP protocol family;
2) Compile webpage files on the server;
3) expose the access address of the resource file;
4) The client initiates a request to obtain resources and complete communication;
From the above comparison, we can see that the key here is to define the protocol, that is, to define the AIDL file process, and then there is an important process, that is, to compile the AIDL file, the compiler creates a Stub class, which is an abstract class inherited from the Binder class, and implemented the interface we defined in the AIDL file, so our operations on the Interface object are eventually converted into the operations on the Stub object, in the specific communication process, Stub objects are completed for us, that is, the tedious tasks of splitting object data and generating object data. Therefore, the communication principle of AIDL only defines the communication rules. The specific implementation depends on the binder. Therefore, the final analysis result is that AIDL splits the data we need to pass through the Binder. On the access end, AIDL assembles the received results into the expected objects and blocks the underlying operations.