Asynchronous callbacks in the Android binder mechanism

Source: Internet
Author: User

"Binder communication is synchronous rather than asynchronous", but when used in practice, it is designed to be client-side synchronous while the server is asynchronous.

Look at the various service classes in the Framwork layer Java source code will know that when the client invokes various methods of the server, usually pass a binder, the binder object for the server to do asynchronous callbacks, The server itself uses handler or queues to make asynchronous processing. In Android, the system service exists as a "manager", like AMS (Activitymanagerservice), which does not create activity, it is the client's activitythread that creates the activity, But whenever Activitythread want to create a acitivty, they must tell Ams,ams to schedule the acitivty to be created. In a simpler example, toast and notificationmanagerservice,toast are responsible for creating and hiding pop-up windows, and when they are displayed and when they are hidden is determined by notificationmanagerservice. Specifically, you can look at this Article Toast window source analysis.

We can see that when the queue is not empty, the synchronization of the client ends when the server finishes executing the Toastrecord into the queue. The server will retrieve the queue entries sequentially and callback the client's show () and Hide () methods. When the service callback show () and Hide (), the client becomes the "server side", when it through handler the real display and hide toast window operations into the message queue, and once again implemented asynchronous operation.

So why design it like this? Take toast, for example, if the service side is not designed to be asynchronous calls + use Binder callback, that when there are 2 toast requests at the same time, in order to achieve the serialization display (toast only one at a time, the display is finished only after the next show), Then the second toast process must block waiting for the first toast display to be hidden, and worse, even the first request must be blocked waiting for the server to send hidden instructions after the display.

(I look at the source code for each IPC call the client will pass a Binder object to the server to feel very strange, then thought it was for "two-way Communication": The server may also require some processing results of the client. But after looking at more code, I think it might be more accurate because of asynchronous callbacks. The above is only a personal understanding, if there are errors please point out. )

Asynchronous callbacks in the Android binder mechanism

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.