Android-Handler, androidhandler

Source: Internet
Author: User

Android-Handler, androidhandler

Reprinted please indicate the source: http://blog.csdn.net/l1028386804/article/details/46952493

I. Handler definition:
It mainly accepts data sent by sub-threads and uses this data with the master thread to update the UI.
Explanation: When an application is started, Android will first enable a main thread (that is, the UI thread). The main thread is the UI control in the management interface for event distribution. For example, if you click a Button, Android will distribute the event to the Button to respond to your operation. If you need a time-consuming operation, such as reading data online or reading a large local file, you cannot put these operations in the main thread, if you put it in the main thread, the interface will be suspended. If the process is not completed in five seconds, you will receive an error message "Force disable" from the Android system ". at this time, we need to put these time-consuming operations in a sub-thread. Because the sub-thread involves UI update, the main Android thread is thread insecure, that is, updating the UI can only be updated in the main thread. Operations in the Child thread are dangerous. at this time, Handler came up to solve this complicated problem. Because Handler runs in the main thread (in the UI thread), it and the sub-thread can transmit data through the Message object, at this time, Handler is responsible for receiving the Message object (containing data) transmitted by the subthread using the sedMessage () method ), put these messages into the main thread queue and update the UI with the main thread.

Ii. Handler features
Handler can distribute the Message object and Runnable object to the main thread. Each Handler instance is bound to the thread that creates the handler (usually located in the main thread ),
It has two functions: (1) scheduling a message or Runnable to execute somewhere in a main thread; (2) arranging an action to execute in different threads.
Methods for sending messages in Handler
Post (Runnable)
PostAtTime (Runnable, long)
PostDelayed (Runnable long)
SendEmptyMessage (int)
SendMessage (Message)
SendMessageAtTime (Message, long)
SendMessageDelayed (Message, long)
The post method above allows you to arrange a Runnable object to the main thread queue,
SendMessage class method, allows you to arrange a Message object with data into the queue, waiting for updates.
Iii. Handler instance
(1) The subclass must inherit the Handler class and override the handleMessage (Message msg) method to accept thread data.
The following is an instance that implements the function of modifying the interface Button content through a thread.

/*** Modify the content of the Button on the interface through a thread * @ author liuyazhuang */public class MyHandlerActivity extends Activity {Button button Button; MyHandler myHandler; protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. handlertest); button = (Button) findViewById (R. id. button); myHandler = new MyHandler (); // when a new Handler instance is created, it is bound to the current thread and message queue, start to distribute data // Handler has two functions: (1) scheduled Message execution and Runnalbe object // (2): let an action be executed in different threads. // It arranges messages, using the following method // post (Runnable) // postAtTime (Runnable, long) // postDelayed (Runnable, long) // sendEmptyMessage (int) // sendMessage (Message); // sendMessageAtTime (Message, long) // sendMessageDelayed (Message, long) // The above method starts with post and allows you to process the Runnable object // sendMessage () allows you to process the Message object (the Message can contain data,) MyThread m = new MyThread (); new Thread (m ). start ();}/*** receives the message and processes the message. This Handler runs with the current main thread **/class MyHandler extends Handler {public MyHandler () {} public MyHandler (low.l) {super (L);} // The subclass must Override this method to accept data @ Override public void handleMessage (Message msg) {// TODO Auto-generated method stub Log. d ("MyHandler", "handleMessage ...... "); super. handleMessage (msg); // you can update UI Bundle B = msg here. getData (); String color = B. getString ("color"); MyHandlerActivity. this. button. append (color) ;}} class MyThread implements Runnable {public void run () {try {Thread. sleep (10000);} catch (InterruptedException e) {// TODO Auto-generated catch block e. printStackTrace ();} Log. d ("thread ....... "," mThread ........ "); Message msg = new Message (); Bundle B = new Bundle (); // store data B. putString ("color", "my"); msg. setData (B); MyHandlerActivity. this. myHandler. sendMessage (msg); // send a message to Handler and update the UI }}}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.