The idea of this article

This article is about how Android uses AIDL for interprocess communication, but I’m not going to start with AIDL. I’ll start with the assumption that Android doesn’t provide AIDL and how we should implement IPC “our way”. After that, This article compares the AIDL approach to IPC and tries to understand the relationship between the code behind AIDL. The following picture is a case interactive diagram of implementing IPC “in your own way”, as follows:

PNG flowchart for LibraryServer to provide cross-process book query service

Image interpretation

LibraryServer and Client in blue are two different apps in this case. LibraryServer can provide the “BookCheckService” service (including getBookInfo and getBookList) in green on the left. LibraryServer also employs “binders” with the ability to interact with remote objects. LibraryServer uses the combination of “BookCheckService” and “Binder” to provide remote services. Clients also “recruit” binders to use remote services. What does “yellow part” mean? In fact, this is just the author’s minimalist description of the Binder mechanism in the underlying communication. Binder mechanism is very complex in fact, and I plan to discuss it in another chapter. The “yellow” part simply explains that the remote service LibraryServer actually registers its own Binder handles with the kernel. The Client actually gets only LibraryServer’s Binder handles and interacts with the remote service through the kernel’s Binder mapping.

Demo key code analysis

image.png


The Client side code

image.png

image.png

The following function is the implementation logic for requesting the remote service “Query book information”

image.png

Also request “get book list” remote service implementation logic is as follows:

image.png

Remote side LibraryServer code

image.png

image.png

image.png

## Test results

image.png

AIDL implementing IPC

In this case, you can use AIDL to communicate with each other in AndroidStudio. For example, you can use AIDL to communicate with each other in AndroidStudio. www.jianshu.com/p/d1fac6cce…

image.png

image.png

image.png

This is a bookCheckService. Java file that I created automatically using AIDL. The following UML can be used to describe the relationship between the classes:

image.png


image.png


image.png


Example source code

Github.com/ZhongXiaoHo…