How does logoff interact with Handler through Message?

Source: Internet
Author: User

The message usage of Android is usually logoff. prepare () logoff. myLooper (); xxxHandler = new Handler () {handleMessage (Message msg ){...}}; logoff. loop (); At the beginning, I was confused about the situation. Based on the name, I couldn't see the two classes that have any relationships. How do I transmit messages? It's okay to know how to use it. Many people should be like me. if you are interested, just click it. logoff. prepare (); There is a sThreadLocal. set (new Looper (); here a new Looper is created and put in ThreadLocal, which is a class for thread data sharing. private Looper () {mQueue = new MessageQueue (); mRun = true; mThread = Thread. currentThread ();} transmits information about the current worker thread, and a new message queue is used to receive messages. public static Looper myLooper () {return sThreadLocal. get ();} This function returns the new logoff (); pub created above. Lic Handler (){... mlogoff = logoff. myloue (); mQueue = mloue. mQueue ;...} here, the Logoff of the current thread is assigned to Handler, Which is related. some may have questions. The above are two different statements of the class logoff. mylogoff (); how can we get the same one? Let's analyze it. There is a global static container static final ThreadLocal <loal> sThreadLocal = new ThreadLocal <loal> () in the logoff class (); go to the ThreadLocal class and check public void set (T value) {Thread currentThread = Thread. currentThread (); Values values = values (currentThread); if (values = null) {values = initializeValues (currentThread);} values. put (this, value);} set the information about the current thread here. look at the get () function; public T get () {// Optimized for the fast pa Th. Thread currentThread = Thread. currentThread (); Values values = values (currentThread); if (values! = Null) {Object [] table = values. table; int index = hash & values. mask; if (this. reference = table [index]) {return (T) table [index + 1] ;}} else {values = initializeValues (currentThread);} return (T) values. getAfterMiss (this);} returns information about the current thread. because ThreadLocal is a template container and declared as ThreadLocal <T>, it is declared as ThreadLocal <lolocal> In logoff, so T = logoff. simply translate the get () and set () above; public void set (loginvalue) {values. put (this, value);} public void lodid get () {return values. get (this);} is clear, and the relationship is like this. let's take a look. logoff. prepare () logoff. myLooper (); xxxHandler = new Handler () {handleMessage (Message msg ){...}}; logoff. loop (); equivalent to logoff. prepare () {sThreadLocal. set (currentThread, new Looper ();} Looper. mylogoff () {sThreadLocal. get (currentThread)} new Handle (){... mLooper = sThreadLocal. get (currentThread); mQueue = mloue. mQueue ;...} if the link has occurred, you can use the Message to communicate with each other.

Related Article

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.