Original Android theory combing the handler correlation mechanism of-NO1 asynchronous processing

Source: Internet
Author: User

1 Basic concepts of handler:

What is handler?

Handler is a callback mechanism for the Android system.

The role of handler?

His role is to communicate between different threads. One is the data traffic that can be used for the child thread and the UI thread, because only the UI thread can update the UI, and if the child thread wants to update the interface, it needs to update the UI with handler by sending a message. The UI thread processes the sent message data in the Handlermessage method. The second is a thread that can be used for handler binding to delay sending messages.

2 Why is it that only the UI thread can update the UI?

Mainly to solve the problem of multithreading concurrency. If all threads can update the UI, in order to resolve concurrency problems, the thread locks the UI object, which can cause the interface to get stuck. It's also easier to design and program by only updating the UI thread.

3 The relationship between Handler, Looper and MessageQueue?

Handler is responsible for sending messages to the bound Looper. And MessageQueue is a message queue container inside looper, Looper loads these messages with MessageQueue message queue. And a dead loop method looper.loop The message from the message queue and sends the extracted message to the Handlermessage method through the DispatchMessage method for processing.

4 can I update the UI directly on a child thread?

There are many articles on the web that say Android lining threads cannot refresh the UI. The key to whether a thread can refresh the UI is if the viewroot belongs to that thread. In fact, a child thread can refresh the UI under certain circumstances. Viewroot instances are created through the Viewrootimpl () method, and Viewroot instances are not established before activity.onresume, so there is no viewroot.checkthread check. So in the early days of the OnCreate method, the UI could be updated in a child thread.

As in the following example:

public class Mainactivity extends Activity {  TextView  textView1;   @Override  protected void onCreate (Bundle savedinstancestate) {          super.oncreate (savedinstancestate);   Setcontentview (r.layout.activity_main);  TextView1 = (TextView) Findviewbyid (r.id.textview1);  New Thread (new Runnable () {public  void run () {  textview1.settext ("Child thread Update UI");  }  }). Start ();  }  }  

Original Android theory combing the handler correlation mechanism of-NO1 asynchronous processing

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.