Android handler, Looper, message, MessageQueue,

Source: Internet
Author: User
Tags message queue

One: Handler,looper,message,messagequeue,thread

Handler: Message processing, responsible for sending message messages (Handler.sendmessage (...) ) and processing messages, for handler processing messages, you need to implement the Handlermessage (Message msg) method, which handles specific messages, such as Finer UI

Looper: Message pump, used to extract messages from MessageQueue, so a looper corresponds to a MessageQueue;

Message: Messages that contain the ID of the message, the message processing object, or the data processed by the same processing as the MessageQueue message queue.

MessageQueue, Message Queuing, messageQueue to save messages sent through handler, of course, the save is not the true meaning of the save, but the chain structure to link the message. Waiting for the extraction of looper

Thread: threads, negative dispatch of the entire message loop, that is, the site execution of the message loop

Two: The relationship between Handler, Looper and MessageQueue

Looper and MessageQueue are one by one corresponding relationships, and creating a looper while creating a messagequeue,handler with them is simply an aggregation relationship, The Looper and MessageQueue in the current thread are referenced in the handler, which shows that the handler can handle a looper and MessageQueue at the same time, provided that the handler are in the same thread

Three: simple example:

Generation of messages:

Message Msg=handler.obtiainmessage ();

Msg.what=what;

Msg.sendtotarget ();

Message Sent:


MessageQueue Queue=looper.myqueue ();

The corresponding message queue is found by this object MessageQueue

if (queue!=null) {
Msg.target=this

Sent = Queue.enqueuemessage (msg, uptimemillis);

}

Note: In handler sendmessageattme (message msg,long time), you can see that handler found a message in MessageQueue in its own thread The target of the message is then set to handler itself, with the intention that the message will find the correct handler.

Three: Extraction

Get the Looper object in handler thread;
Looper Looper=handler1.getlooper ();

The corresponding message queue is found by this object MessageQueue
MessageQueue Queue=looper.myqueue ();

While (true) {

Message msg = Queue.next (); Constantly getting messages from message queues

if (msg! = null) {

if (msg.target = = null) {

Return

 }

msg.target.dispatchMessage (msg);

msg.recycle ();

 }

}

From here we can see constantly getting the message from the message queue and then going through the message with the information carried in the target, looking for the correct handler to process the message

Four: Treatment:

Handler = new Handler () {

Replication Handlermessage (Message msg);

}

So far, we see, a message through the handler send, MessageQueue the queue, Looper extraction, and again back to the arms of handler. And the circle around it just helps us turn the synchronous operation into an asynchronous operation.

Small instance: With the new UI

First in the main thread if we do not create the Looper object using handler, the system will default to the Looper object already created in the main thread, but this looper cannot accept the message if we use handler instead of the handler object in the non-UI thread.

The usual practice is to:

Thread thread = new Thread () {

@Override
public void Run () {

Before creating handler we need to prepare a looper first


Looper.prepare ();
Handler Handler = new Handler () {

@Override
public void Handlemessage (Message msg) {
TODO auto-generated Method Stub
Super.handlemessage (msg);
}

};
Super.run ();
}

};

Start the Looper (run up) so that you can extract the message from the MessageQueue so that handler can run properly
Looper.loop ();
Thread.Start ();

}

Note: Handler processing messages always run in the thread that created the handler, and in our message handling, there are no shortage of updates to the UI, and an incorrect thread that directly updates the UI throws an exception, so we need to pay attention to the thread where the handler is located in real time.

Summary:

    • The processing run of the handler is run in the thread that created the handler
    • A looper corresponds to a MessageQueue
    • One thread corresponds to a looper
    • A looper can correspond to multiple handler (these handler are in the same thread)

Android handler, Looper, message, MessageQueue,

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.