Android development notes Handler usage summary

  • 2020-05-05 11:51:14
  • OfStack

Definition of Handler:

mainly accepts data sent by child threads and updates UI.
with this data in conjunction with the main thread Explanation: when the application starts, Android will first start a main thread (UI thread), which will distribute events to the UI control in the administrative interface. For example, if you click on Button, Android will distribute events to Button in response to your actions. If a time-consuming operation is required, for example: Networking read data, or read the local large a file, you can't put these actions in the main thread, if you put in the main thread, the interface will appear feign death phenomenon, if 5 seconds is not yet complete, will receive an error message Android system "closed". This time we need to put these time-consuming operation, placed in a child thread, because the child thread involves UI updates, Android main thread is the thread unsafe, that is to say, the updated UI can only in the main thread, Child thread operation is dangerous. In this time, Handler came to solve this complex problem, because Handler run in the main thread (UI thread), it has to do with the child thread can be passed through Message object data, this time, Handler will send it to accept the child thread (the child thread sedMessage () method and brother) Message object, (containing data), put the news in the thread queue, cooperate with the main thread in updating UI.

Some features of

handler can distribute Message objects and Runnable objects to the main thread. Each instance of Handler is bound to the thread that created it (usually on the main thread),
It has two functions: (1) scheduling a message or Runnable to execute somewhere in a main thread, and (2) scheduling an action to execute
in a different thread Some methods of distributing messages in Handler,
postDelayed(Runnable long)
The post class method above allows you to queue an Runnable object to the main thread,
sendMessage class method, which allows you to queue an Message object with data, waiting for updates.

3, Handler example

(1) subclass needs to inherit the Handler class and override the handleMessage(Message msg) method to accept the thread data
The following is an example of what it does: modify the contents of Button via a thread,

public class MyHandlerActivity extends Activity {
Button button;
MyHandler myHandler;
protected void onCreate(Bundle savedInstanceState) {
button = (Button) findViewById(;
myHandler = new MyHandler();
// When creating a new one Handler When an instance , It binds to the queue of the current thread and message , Start distributing data
// Handler It does two things , (1) : Regularly perform Message and Runnalbe object
// (2): Make an action , Execute in different threads .
// It arranges messages , In the following way
// post(Runnable)
// postAtTime(Runnable,long)
// postDelayed(Runnable,long)
// sendEmptyMessage(int)
// sendMessage(Message);
// sendMessageAtTime(Message,long)
// sendMessageDelayed(Message,long)
// The above methods are post The beginning allows you to handle Runnable object
//sendMessage() You are allowed to deal with Message object (Message Can contain data ,)
MyThread m = new MyThread();
new Thread(m).start();
* Receive a message , Process the message , this Handler Will run with the current main thread
* */
class MyHandler extends Handler {
public MyHandler() {
public MyHandler(Looper L) {
// Subclasses must override this method , Receive data
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
Log.d("MyHandler", "handleMessage......");
// It can be updated here UI
Bundle b = msg.getData();
String color = b.getString("color");
class MyThread implements Runnable {
public void run() {
try {
} catch (InterruptedException e) {
// TODO Auto-generated catch block
Log.d("thread.......", "mThread........");
Message msg = new Message();
Bundle b = new Bundle();// To store data
b.putString("color", " my ");
MyHandlerActivity.this.myHandler.sendMessage(msg); // to Handler Send a message , update UI

Related articles: