Android uses Aidl to realize communication between two apps

Today, we have developed a function to complete the communication between two apps through Android's Aidl mechanism. The functional requirements are very simple. A client app is called client and a server app is called ordermanager; The client is responsible for displaying the order list and adding orders to the server; The server is responsible for managing orders and can provide external order information.

Don't gossip. Let's go directly to the code below.

1. Write bean classes.

First, define the orderbean class in the client. The fields are very simple. Note that you need to implement the Parcelable interface:

2. Define the Aidl file. Aidl files are stored in Android studio in the following path: client / SRC / Aidl /

3. Define the layout file and write mainactivity. This is very simple. Button and recyclerview can be combined:

And write an item_ Order.xml is used to display recyclerview. It is very simple and is omitted here.

4. Write server code

There are two main things to do here. The first is to migrate the Aidl file and bean class, migrate the Aidl file and orderbean in the client, and keep the file path consistent. The second is to write the orderservice.java class.

The orderservice service is configured in androidmanifest.xml as follows:

The code writing is completed. When running, you need to run the server app first, and then the client. If the orderservice service is broken during the running process, the client will be notified through the death agent, and then rebind.

The operation is as follows:

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