Handle message mechanism

Source: Internet
Author: User

The main role of the handle class in Android:

1. Sending messages to a newly-started thread

2. Get and process messages on the main thread

Why use a mechanism such as handle:

Because UI operations are not thread-safe in an Android system, it can cause thread-safety problems if multiple threads concurrently operate on the same component. To solve this problem, Android has a rule that allows only UI threads to modify the properties of the UI component, which means that a single-threaded model is required, which results in a program that is called a long-time data update in the UI interface, and so on. If the program is not completed in 20 seconds, it will be forced to close. So, for example, when another thread modifies the UI component, it needs to use the handler message mechanism.


Handle several ways to send and process messages:

1. void handlemessage (Message msg): The method that processes the message, which is usually overridden.

2.final boolean hasmessage (int what): Checks whether Message Queuing contains a message with the What property is the specified value

3.final boolean hasmessage (int what, Object object): Checks whether Message Queuing contains a message with a value specified by the What's good Object property

4.sendEmptyMessage (int): Send an empty message

5.final Boolean send emptymessagedelayed (int what, long Delaymillis): Specifies how many milliseconds to send an empty message

6.final boolean sendMessage (Message msg): Send Message now

7.final boolean sendmessagedelayed (Message Msg,long Delaymillis): Sends a message after a few seconds


Several components working with handle Looper, MessageQueue the respective functions:

1.Handler: It sends the message to the Looper managed MessageQueue and handles the Looper message.

2.MessageQueue: Advanced Way to manage message

3.Looper: Each thread has only one Looper, such as UI thread, the system initializes a Looper object by default, it manages MessageQueue, constantly takes messages from MessageQueue, and

The corresponding message is divided into handler processing


Steps to use handler in a thread:

1. Call Looper's prepare () method to create a Looper object for the current thread, and when the Looper object is created, its constructor automatically creates the corresponding MessageQueue

2. Create an instance of the handler subclass, overriding the Handlemessage () method, which handles messages in addition to the UI thread for external threads

3. Call Looper's Loop () method to start the Looper

From http://blog.csdn.net/x605940745/article/details/13001279, more detailed code can be viewed in

More details on http://www.cnblogs.com/angeldevil/p/3340644.html

Handle message 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.