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)