Android thread message Loop Mechanism

Source: Internet
Author: User
Android. OS. logoff: logoff is used to encapsulate the message loop in the android thread. By default, the next thread does not have a message loop. You need to call logoff. prepare () to create a message loop for the thread and call logoff. loop () to make the message loop take effect, get the message from the message queue, and process the message.

Note: written in logoff. the code after loop () will not be executed immediately. After the call, mhandler. getlooper (). after quit (), the loop will be aborted, and the subsequent code can be run. The logoff object stores messages and events through messagequeue. A thread can have only one logoff corresponding to one messagequeue.


The following is a typical loginthread implementation in Android APIs:
// Handler's default constructor without parameters: new handler (), which actually obtains the message loop in the current thread through logoff. mylogoff,
// By default, the thread does not have a message loop, so you need to call logoff. prepare () to create a message loop for the thread, and then pass, logoff. loop () to make the message loop take effect.

Class looperthread extends thread {
Public handler mhandler;

Public void run (){
Logoff. Prepare ();

Mhandler = new handler (){
Public void handlemessage (Message MSG ){
// Process incoming messages here
}
};

Logoff. Loop ();
}
}

In addition, the mainui thread of the activity has a message queue by default. Therefore, when creating a handler in an activity, you do not need to call logoff. Prepare () first ().

Android. OS. Handler:

Handler is used to bind with the thread to send messages to the thread's message loop, receive messages, and process messages.

The handler constructor without parameters is as follows:

Public handler (){

If (find_potential_leaks ){
Final class <? Extends handler> Klass = getclass ();
If (Klass. isanonymousclass () | Klass. ismemberclass () | Klass. islocalclass ())&&
(Klass. getmodifiers () & modifier. Static) = 0 ){
Log. W (TAG, "The following handler class shocould be static or leaks might occur:" +
Klass. getcanonicalname ());
}
}

Mlogoff = logoff. mylogoff ();
If (mloiter = NULL ){

// If there is no message loop in the current thread, the system throws an exception. That is, if you want to use handler to process messages in a thread, you need to call looer. Prepare () to create a message loop, which is not required by the mainui thread.

Throw new runtimeexception (
"Can't create handler inside thread that has not called logoff. Prepare ()");
}
Mqueue = mloue. mqueue;
Mcallback = NULL;
}

Use the following function to send messages or runnable messages to a thread:1、post(Runnable),postAtTime(Runnable, long),
postDelayed(Runnable, long);When a thread receives a runnable object, the call is immediately or delayed.2、sendEmptyMessage(int),
sendMessage(Message),sendMessageAtTime(Message, long), And
sendMessageDelayed(Message, long)。After receiving these messages, the thread processes the received messages based on the handlemessage reconstructed by your handler. In addition, there can be multiple handler objects in an activity main thread, but messagequeueloading has only one and the corresponding logoff has only one.

 

Related Article

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.