Android interview question 2: why is there a message mechanism? Android interview message mechanism

Source: Internet
Author: User

Android interview question 2: why is there a message mechanism? Android interview message mechanism

In Android UI development, Handler is often used to control the interface changes of the main UI program. For Handler functions, we can conclude that it works with other threads to receive messages from other threads and update the content of the main UI thread through the received messages.



Assume that there is a button on the UI interface. When you click this button, the network connection is established, take a string on the network and display it to a TextView on the interface. A problem occurs. If the network connection delay is too large, it may be 10 seconds or longer, then our interface will remain in the suspended state, and if this time exceeds 5 seconds, the program will be abnormal.


At this time, we will think of using threads to complete the above work, that is, when the button is pressed, a new thread is enabled to complete the network connection, and the results are updated to the UI. However, another problem occurs at this time. In Android, the main thread is non-thread-safe, that is, the UI update can only be completed in this thread, other threads cannot directly operate on the main thread.


To solve the preceding problems, Android has designed the Handler mechanism. Handler is responsible for communicating with the sub-thread, so that the sub-thread and the main thread can establish a collaboration bridge, the problem of Android UI update is solved perfectly. Next we will give an example to illustrate the basic usage of Handler.


A. Working Principle of Handler

Generally, Handler is bound to the main thread, and a new thread is created on the event trigger to complete some time-consuming operations. After the work in the subthread is completed, the Handler will send a complete signal to the Handler, And the Handler will update the main UI after receiving the signal.

B. Handler and subthread collaboration instance

Class MyHandler extends Handler {public MyHandler () {} public MyHandler (low.l) {super (L) ;}// override the handleMessage method, accept the data and update the UI @ Override public void handleMessage (Message msg) {super. handleMessage (msg); // UI operation based on msg content here }}

2. Implementation of sub-threads

Class MyThread implements Runnable {public void run () {Message msg = new Message (); Bundle B = new Bundle (); B. putString ("cmd", "update"); msg. setData (B); MainActivity. this. myHandler. sendMessage (msg); // notify Handler to update the UI }}

With the above two implementations, we only need to declare the MyHandler Instance Object in MainActivity to complete the communication between threads and interface update operations.

MyHandler myHandler = newMyHandler ();



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.