Use aidl to implement inter-process communication (IPC) by the binder)

Source: Internet
Author: User

Before writing this article, I have been thinking about these issues:

Question 1: Why do I need IPC?

Question 2: What is binder?

Question 3: What is aidl?

Question 4: What are the differences and connections between IPC and activity intent, broadcast, content provider, and service?

If you can easily answer all the questions, congratulations! You have learned to answer the question ^-^.

The answer is as follows:

1. Why do I need IPC? IPC refers to inter-process communication. Because Linux-based Android does not support accessing data from another process from the current process, it must be implemented through specific channels. Android uses binder to implement inter-process communication, the specific application scenario is generally that there is an independent service end, access the service end (another independent process) from the client (an independent process), such as the music playing service that can be run in the background, and the music playback control interface (client ).

2. binder means to wrap the processes together. It is a method for Android to implement IPC. It only needs to test the memory once, cross-process function calls can be implemented with high efficiency and fast speed (compared with socket/pipeline/Message Queue). For details, refer to here.

3. aidl is a tool to help implement binder communication. With just a few simple function declarations, aidl will help generate a Java file, including an access interface for proxy and stub, and the parcel used for communication between them, and ensure the consistency of input and output of parcel. The proxy is used for client processes, and stub is used for service processes, experienced programmers can write proxy and stub code by themselves without using aidl. Obviously, I am not such a person. At the same time, even experienced programmers, in order to save time, will choose to use aidl, Android source code can be seen.

4. What are the differences and connections between IPC and activity intent, broadcast, content provider, and service?

In the Android system, there is no common memory area for multiple applications to share and store data. contentprovider enables multiple applications to share data across processes, IPC refers to mutual calls and communication between two or more programs. It can not only share data, but also share a public function. A service can have several clients to access and call.

Intent can carry some data to open another program (process), but it cannot perform real-time communication between processes.

Broadcast can implement large-scale anti-DDoS broadcast, but it cannot communicate between two-way processes. IPC (binder) can.

A service is a program running in the android background. If you want to interact with it, you can directly access the local service or use Messenger (based on aidl ), supports single thread. One is the aidl method, which supports simultaneous access by multiple threads.

Well, with these preparations, I think I should write some programs and write a music player with a control terminal (client.

In order to save time, I found a code for playing music that has been written for more than half of the time on GitHub. The function is complete for more than half of the time, and the program looks like a bunch of bugs. I used aidl to rewrite the playback service interface and wrote a simple control terminal. In addition, this program also has a special feature for me: The company nap ghost weapons ==>> sound insulation and hypnosis timing machine, about 30 minutes of nap every day, select 10 songs (about 30 minutes), put on your earphones, and start to be hypnotized in the voice. After the 10 songs are completed, the music stops, then I woke up with the "Voice of human flesh alarm clock". To sum up, I had to have a custom music player for it migrant workers :)

Here is a good article on how to use aidl. I can understand it with my source code.

Ref0: http://blog.csdn.net/universus/article/details/6211589

Ref1: parcel http://prasanta-paul.blogspot.com/2010/06/android-parcelable-example.html

Ref2: http://myqdroid.blog.51cto.com/2057579/394189

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.