Android Handler (1)

Source: Internet
Author: User

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

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.