Android Looper.prepare () (reproduced)

Source: Internet
Author: User

Looper is used to encapsulate a message loop in an Android thread, by default a thread is not present in the message loop, and it needs to call Looper.prepare () to create a message loop for the thread, calling Looper.loop () Make the message loop work, take the message from the message queue, and process the message.

Note: code written after Looper.loop () will not be executed immediately, and after the call to Mhandler.getlooper (). Quit (), loop will not abort until the code can be run. The Looper object holds messages and events through MessageQueue. A thread can have only one looper, corresponding to a MessageQueue.


The following is a typical Looper thread implementation in the Android API:

Handler the default constructor with no parameters: New Handler (), which actually obtains the message loop in the current thread through Looper.mylooper ().
By default, threads do not have a message loop, so call Looper.prepare () to create a message loop for the thread, and then pass, Looper.loop () to make the message loop work.


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 ();
}


In addition, the activity's Mainui thread has a message queue by default. Therefore, when you create a new handler in activity, you do not need to call Looper.prepare () first.



Looper is used to encapsulate a message loop in an Android thread, by default a thread is not present in the message loop, and it needs to call Looper.prepare () to create a message loop for the thread, calling Looper.loop () Make the message loop work, take the message from the message queue, and process the message.

Note: code written after Looper.loop () will not be executed immediately, and after the call to Mhandler.getlooper (). Quit (), loop will not abort until the code can be run. The Looper object holds messages and events through MessageQueue. A thread can have only one looper, corresponding to a MessageQueue.


The following is a typical Looper thread implementation in the Android API:

Handler the default constructor with no parameters: New Handler (), which actually obtains the message loop in the current thread through Looper.mylooper ().
By default, threads do not have a message loop, so call Looper.prepare () to create a message loop for the thread, and then pass, Looper.loop () to make the message loop work.


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 ();
}


In addition, the activity's Mainui thread has a message queue by default. Therefore, when you create a new handler in activity, you do not need to call Looper.prepare () first.


Looper is used to encapsulate a message loop in an Android thread, by default a thread is not present in the message loop, and it needs to call Looper.prepare () to create a message loop for the thread, calling Looper.loop () Make the message loop work, take the message from the message queue, and process the message.

Note: code written after Looper.loop () will not be executed immediately, and after the call to Mhandler.getlooper (). Quit (), loop will not abort until the code can be run. The Looper object holds messages and events through MessageQueue. A thread can have only one looper, corresponding to a MessageQueue.


The following is a typical Looper thread implementation in the Android API:

Handler the default constructor with no parameters: New Handler (), which actually obtains the message loop in the current thread through Looper.mylooper ().
By default, threads do not have a message loop, so call Looper.prepare () to create a message loop for the thread, and then pass, Looper.loop () to make the message loop work.


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 ();
}


In addition, the activity's Mainui thread has a message queue by default. Therefore, when you create a new handler in activity, you do not need to call Looper.prepare () first.

Reprint Address: http://blog.sina.com.cn/s/blog_9de87bd501012kdm.html

Android Looper.prepare () (reproduced)

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.