Android Development notes Handler usage Summary

Source: Internet
Author: User

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.Copy codeThe Code is as follows: public class MyHandlerActivity extends Activity {
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 queue of the current thread and message to start distributing data.
// Handler has two functions: (1) scheduled Message execution and Runnalbe object execution
// (2): let an action be executed in different threads.
// It schedules 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 Runnable objects
// SendMessage () allows you to process Message objects (messages can contain data ,)
MyThread m = new MyThread ();
New Thread (m). start ();
}
/**
* Receives and processes the message. The Handler runs together with the current main thread.
**/
Class MyHandler extends Handler {
Public MyHandler (){
}
Public MyHandler (low.l ){
Super (L );
}
// 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 );
// The UI can be updated here
Bundle B = msg. 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 to update the UI
}
}

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.