Android Basics (vi) Message processing mechanism for Android

Source: Internet
Author: User

The message processing mechanism in Android consists of four parts: message, Handler, MessageQueue, and Looper, and MessageQueue encapsulated in Looper, we generally do not deal directly with MQ.

First, Looper

Looper literally means "circulator", which is designed to make a normal thread into a looper thread . The so-called Looper thread is the thread that loops work. In program development (especially in GUI development), we often need a thread to loop continuously, and once a new task is executed, the execution continues to wait for the next task, which is the looper thread.

There is only one Looper object in a thread, one Looper object has only one MessageQueue, and one thread can have more than one hander, which can be processed as needed for hander to be set differently.

So how do you turn a thread into a looper thread? It's really simple, just add the following two lines of code to the thread:

 public  class  looperthread extends   public  void   run () { //   Looper.prepare ()                ;  //  ... Other processing, such as instantiating handler  //  start looping Message Queuing  Looper.loop (); }}

The Looper.prepare () method is called to threadlocal for the current thread (see http://blog.csdn.net/qjyong/article/details/for an explanation of threadlocal 2158097) Set the value (as I understand it is to create a new looper for the current thread, and when a thread has a Looper object the thread can become a looper thread). Then call the Looper.loop () method to let the Looper thread start to work, Looper will continue to remove the queue from their own MessageQueue message processing.

There are other methods besides the prepare () and loop () Looper classes:

Looper.mylooper () Gets the current thread Looper object

GetThread () Gets the thread that the Looper object belongs to

The Quit () method ends the Looper loop

Second, Handler

Handler plays the role of adding messages and processing messages to MQ (handling only messages sent by itself), notifying MQ that it is going to perform a task (sendMessage) and performing the task (Handlemessage) at loop to its own. The entire process is asynchronous . Handler is created with a looper associated with it, the default constructor associates the looper of the current thread, but this can be set.

With the handler, we can use,,,, post(Runnable) postAtTime(Runnable, long) postDelayed(Runnable, long) sendEmptyMessage(int)

sendMessage(Message), sendMessageAtTime(Message, long) and sendMessageDelayed(Message, long) These methods send messages to MQ; use DispatchMessage (

message msg) with Method handlemessage (Message msg). Look at these APIs you may think handler can send two kinds of messages, one is Runnable object, one is a message object, this is a visual understanding, but in fact, the Runnable object of post is encapsulated into a message object, generally we Use Handler's SendMessage () method to send messages to the message queue, using the Handlemessage () method to process messages sent by the current handler in the message queue .

The role of handler:

1.handler can send messages on any thread that is added to the handler associated MQ.

2.handler is the processing of messages in its associated looper thread .

This is the question of why Android cannot update the UI in other non-main threads. the main thread of Android is also a looper threading , and the handler we create in it will be associated with the main thread MQ by default. Thus, one of the solution of using handler is to create handler in the activity and pass its references to the worker Thread,worker When thread finishes the task, it uses handler to send a message informing the activity update UI ( My understanding is that the main thread is automatically created with corresponding handler, Looper, and MessageQueue when it is turned on. The action is that when the interface is modified, a message is sent to the message queue for processing by handler, and the handler, Looper, and MessageQueue are not created automatically when the child thread is turned on. So when the interface constant change is no message to notify the current main thread of the handler to handle this action, so we have to create a child thread in the Looper and MessageQueue using the main thread of the handler send messages ), such as:

Third, Message

In the entire message processing mechanism, message is called a task, encapsulating the information carried by the task and the handler that handles the task.

1. Although the message has a public default constructor, you should use Message.obtain () to obtain an empty message object from the message pool to conserve resources.

2. If your message only needs to carry simple int information, prioritize using MESSAGE.ARG1 and message.arg2 to pass information, which is more memory-saving than using bundles

3. Use Message.what to identify information so that you can handle the message in different ways.

Android Basics (vi) Message handling mechanism for Android

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.