This article is also super useful, save it and forget to see it again.
In developing Android applications, sometimes we need to create some resident threads in the application that do not periodically perform computational tasks that do not require interaction with the application interface. If these sub-threads have message loops, they can be resident in the application to perform some computational tasks irregularly: When we need to use these sub-threads to perform tasks, we send a message to this child thread's message queue. Then we can execute our computational task in the message loop of the child thread, on Android, where the thread is divided into a message loop thread and a thread without a message loop, a thread with a message loop will generally have a looper, which is the new concept of Android. Our main thread (the UI thread) is the one that threads the message loop. For this kind of message loop mechanism, we introduce a new mechanism handler, we have the message loop, we must send the corresponding message in the message loop, the custom message will have its own corresponding processing, the message is sent and purged, the processing of the message, these are encapsulated in the handler inside, Note that handler is only for those threads that have looper, whether it's the UI thread or the child thread, as long as you have looper, I can add something to your message queue and handle it accordingly. If you create a new handler inside a child thread, an error occurs. The reason is that one thread corresponds to one or 0 looper and Messageque. Handler is a message mechanism, and the child thread is enabled by default without the Looper object (mainline threads), so when the child thread uses the handler object, it first uses looper.prepare (), enables a looper, and then creates a new handler object, Then use Looper.loop (). At this point the child thread has its own looper, which can accept and process the message. Android.os.Looper:Looper is used to encapsulate a message loop in an Android thread, and by default a thread does not have a message loop, and it needs to call Looper.prepare () to create a message loop for the thread. Call Looper.loop () tomakes the message loop work, fetching messages from the message queue, and processing messages.
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() creates a message loop for the thread and then passes, Looper.loop () to make the message loop work.
Class Looperthread Extends Thread {
Public HandlerMhandler;
Public voidRun() {
Looper.Prepare();
Mhandler= New Handler() {
public void Handlemessage (message Msg) {
// Process incoming Messages Here
}
};
looper. }
}
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.
Android.os.Handler:
Handler is used to bind to threads to send messages, accept messages, and process messages inside a thread's message loop.
The following is a handler constructor with no parameters:
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 should be static or leaks might occur: "+
& nbsp; klass.getcanonicalname ());
}
}
Mlooper = Looper.mylooper ();
if (Mlooper = = null) {
If there is no message loop inside the current thread, the system throws an exception. That is, in a thread, if you want to use handler to process the message, you need to call Looer.prepare () to create the message loop, and the Mainui thread does not need it.
throw New RuntimeException (
"Can ' t create handler inside thread that have not called looper.prepare ()");
}
Mqueue = Mlooper.mqueue;
Mcallback = null;
}
Send a message or runnable to a thread by using the following function:1、post(Runnable),postAtTime(Runnable, long),postDelayed(Runnable, long);Called immediately after a thread receives a Runnable object or a certain delay.2、sendEmptyMessage(int),sendMessage(Message),sendMessageAtTime(Message, long), andsendMessageDelayed(Message, long)。When the thread receives these messages, it is processed according to the received message based on your handler refactoring handlemessage (message). In addition, an activity main thread can have multiple handler objects, but Messagequeuelooper is only one, the corresponding Looper is also only one.
. MessageQueue
As the name implies is the message queue, what to do with the Looper class of course, the static member function Preparemainlooper is a specialized application of the main thread call, To make it easy for other places to get the message loop object in the main thread of the application through the Getmainlooper function of the Looper class. Original link: http://blog.csdn.net/chengyingzhilian/article/details/7646895
Android Handler threading Principles detailed