Android-handler, Looper, MessageQueue, message

Source: Internet
Author: User

Handler created, must have a looper, the main thread has created itself. Other threads need to be created by themselves, not by default. Create a method

1. This method is to create a system-defined handlerthread, which is run in a non-UI thread, has created a good looper, directly use it. Create handler have multiple constructors, see the source code

Handlerthread Threadhandler = new Handlerthread ("1111");
Threadhandler.start ();
MyHandler = new MyHandler (Threadhandler.getlooper ());

   2. Create a new thread yourself, call Looper.prepare () in the threads, then write the message callback method, and then call Looper.loop ();
Class Looperthread extends Thread {
Public Handler Mhandler;
  
public void Run () {
Looper.prepare ();

Mhandler = new Handler () {
public void Handlemessage (Message msg) {
Process incoming messages here
}
};

Looper.loop ();
}
}
Why must there be a looper?
Because Looper is required in the parameterless constructor of handler, an exception is thrown if it is empty.
How do I set looper?
Call Looper.prepare (); This will determine if the current thread is already looper, and some will throw an exception, which means that a thread can have only one looper.
What is a message and what is the use of it?
For storing messages, it has many properties. Specifically, Target, which represents the Handler,next attribute of handling it, is the message queue constructed using the linked list structure, and Message.next represents the next of the current message, in terms of time, which cannot be reversed, and he has no prev attribute.
What's the MessageQueue, what's the use?
Used to manipulate messages, such as handler to send messages, he needs to string the messages in sequence. And after processing the message, he needs to remove the message from the message queue and so on.
What's the use of handler?
Send and process messages. Handler sends messages through SendMessage and so on, and is eventually processed by Sendmessageattime to send to MessageQueue. The Looper callback function Msg.target.dispatchMessage (msg)-->handler is then processed specifically by the Handlemessage () method.
Handler can also use the Post method to pass the runnable to Message Queuing messages with the callback attribute, in the DispatchMessage () method of Looper, if it is runnable directly run ();

Finally there is a question: what kind of mechanism can let Looper loop to deal with the message?

While (true) {

Message msg = Queue.next (); //might block

if (msg! = null) {

    if (msg.target = = null) {

      Return

}

    if (me.mlogging!= null) me.mLogging.println ( ">>>>> dispatching to" + Msg.target + " "  

+ Msg.callback + ":" + msg.what );

Msg.target.dispatchMessage (msg);

    if (me.mlogging!= null) me.mLogging.println ( "<<<<< finished to" + Msg.target + " "  

+ Msg.callback);

Msg.recycle ();

}

See Message msg = Queue.next (); //might block this sentence, get the message to be processed, here may be blocked, there is no message when the loop waits until MessageQueue exits


Android-handler, Looper, MessageQueue, message

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.