Android Handler Message delivery mechanism

Source: Internet
Author: User

In the Android system, the class handler has the following two functions mainly.

Sends a message in a newly-started thread.

Gets and processes the message in the main thread.

Class handler When you do this, you first send a message in a newly-started thread, and then you get and process the message in the main path. But

This process involves a problem: when does a newly-started thread send a message? When does the main thread get and process messages? The timing is clearly not in control.

In order for the main program to "timely" handle the message sent by the newly-started thread, it is clear that the callback is the only way to implement it-developers simply rewrite the handler class

The method that processes the message, and when the newly-started thread sends a message, the message is sent to the MessageQueue associated with it, and handler continuously fetches and processes the message from MessageQueue.

That is, the method that processes the message in the handler class is callback.

The Android system does not allow access to the interface components in the activity in the new thread, so the program can only send a message to the new thread informing the system to update the UI.

Android Handler Message delivery 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.