Android theory-Handler mechanism of No1 asynchronous processing, android-no1

Source: Internet
Author: User

Android theory-Handler mechanism of No1 asynchronous processing, android-no1

1 Basic concepts of Handler:

What is handler?

Handler is a callback mechanism for Android systems.

What is the role of handler?

It is used for communication between different threads. First, it can be used for data communication between the subthread and the UI thread. Because only the UI thread can update the UI, if the subthread wants to update the interface, it needs to use Handler to update the UI by sending a Message, the UI thread processes the transmitted Message data in the handlerMessage method. Second, it can be used for the thread bound by Handler to delay message sending.

2. Why can only the UI thread update the UI?

It is mainly used to solve the problem of multi-thread concurrency. If all threads can update the UI, the thread locks the UI object to solve the concurrency problem, and the deadlock may cause the interface to shut down. In addition, only the UI thread is updated to make the design and programming easier.

3 What is the relationship between Handler, logoff, and MessageQueue?

Handler is responsible for sending messages to the bound logoff. MessageQueue is a message queue container within logoff. logoff uses MessageQueue message queue to load these messages, and uses an infinite loop logoff method. loop extracts messages from the message queue and sends them to the handlerMessage method through the dispatchMessage method for processing.

4 Can I directly update the UI in the subthread?

I can see many articles on the Internet that sub-threads in android cannot refresh the UI. In fact, the key to whether a thread can refresh the UI is whether ViewRoot belongs to this thread. In fact, sub-threads can refresh the UI in specific circumstances. The ViewRoot instance must be created using the ViewRootImpl () method. Before Activity. onResume, The ViewRoot instance is not created, so there is no ViewRoot. checkThread check. Therefore, in the early stage of the onCreate method, the UI can be updated in the subthread.

For example:

?
1234567891011121314 public class MainActivity extendsActivity { TextView textView1;@Overrideprotectedvoid 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("Subthread update UI");} }).start(); } }

References:

Android learning notes (60) Use Bundle to teach android learning notes between activities (59) android development start and close activityandroid learning notes (58) Activity learning process more android development tutorials

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.