Aidl interprocess communication example in Android

There should be no more introduction about IPC. Processes in the Android system cannot share memory. What if two different applications need to communicate? For example, a project of the company needs to be updated, and the product needs to develop a plug-in depending on the current project. However, the plug-in function and interface are complex and cannot be in the same process with the current project. At the same time, it also needs to use some things already written in the current project, Then, because the newly developed plug-in attached to the current project is not a process with the current project, it cannot share memory. Therefore, it is necessary to provide some mechanisms for data communication between different processes. This mechanism is Aidl.

1、 A simple example of Aidl in Android

If so, now there is a project that provides more mature calculation methods, and now I want to develop a software. One module wants to use a calculation class, and I don't want to rewrite it, so it can be realized through Aidl. Suppose that the developed program that provides mature computing classes is called aidlcalculatedemoserver (equivalent to server), and the program I want to write is called aidlcalculatedemoclient (equivalent to client), which is similar to the client server mode. First of all, let's take a look at the engineering structure diagram:

Now suppose that the program you write needs to call the operation interface of the server, enter num1 and num2 for remote operation, call the interface of the server, and return the result to the client after the server completes the operation. The effect diagram is as follows:

After compilation, it is found that the directory structure is shown in Figure 1-1 and Figure 1-2: Gen / com example. aidl. Calculateinterface is added to calculate Java file, as follows:

Defining an interface depends on the code of the server and client. The server mainly depends on the calculateservice code, a class that inherits service, which gives practical significance to the interface in Aidl, as follows:

Then you can see that the key services are provided. How can the client access them? Bind the service and a serviceconnection class, as follows:

In this way, the great work has been basically completed. Finally, let's take a look at the server configuration file:

2、 Notes for writing Aidl

1. The package of Aidl interface files on the client and server must be the same

2. A service class is required

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
分享
二维码
< <上一篇
下一篇>>