Android applications are similar to Win32 message processing mechanisms and are also message-driven. It also involves messages, message queues, message loops, and message processors at the application layer that process messages.
It is a static relationship between logoff, Handler, Message, and MessageQueue.
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1055145T5-0.jpg "title =" androidlearning and deep-1.jpg "/>
Next, we will learn more about several related components. First, we will start to understand the entire message processing process from the message loop logoff, and gradually learn more about other components in this process.
Logoff mainly encapsulates the message loop (loop method) and maintains the associated message queue. Combined with the logoff implementation source code, we can see that mmainlogoff is a static instance of logoff, it exists in the main thread of the application. Of course, you can also create your own logoff through the prepare and loop of logoff, as shown in the following example:
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(); }}
You can start Looper Thread in the application as follows:
New LooperThread (). start ();
In fact, this is to start a new thread that executes LooperThread. run (). Next, we will analyze the two loops' key calls prepare and loop, which also start the message loop from the loop.
1) logoff. prepare
Public static void prepare () {// here, only one prepare in a thread can be prepare once if (sThreadLocal. get ()! = Null) {throw new RuntimeException ("Only one logoff may be created per thread");} // construct a logoff object and set it to sThreadLocal in the thread local variable of the calling thread. set (new logoff ());}
Let's take a look at the structure of the new logoff instance in prepare:
private Looper() { mQueue = new MessageQueue(); mRun = true; mThread = Thread.currentThread();}
First, it is a private object, which limits that only the prepare can be used to create a logoff external, and further ensures that only one logoff object can exist in a thread.
In addition, we can see that a message queue is created here and the thread to which the logoff belongs is obtained.
This article is from the "quiet Madman" blog, please be sure to keep this source http://quietmadman.blog.51cto.com/3269500/1282250