SoFunction
Updated on 2025-04-09

Android Handler details

Definition of Handler
It mainly accepts data sent by the child thread and uses this data to cooperate with the main thread to update the UI.
Explanation: When the application starts, Android will first open a main thread (that is, the UI thread). The main thread is the UI control in the management interface to distribute events.

For example, if you click on a Button, Android will distribute events to the Button to respond to your operations. If you need a time-consuming operation at this time, for example: when reading data online or reading a large local file, you cannot put these operations in the main thread. If you put them in the main thread, the interface will have a fake death phenomenon. If it is not completed in 5 seconds, you will receive an error message from the Android system "Forced Close". At this time, we need to put these time-consuming operations in a child thread, because the child thread involves UI updates, and the Android main thread is thread-unsafe, that is, updating the UI can only be updated in the main thread, and operations in the child thread are dangerous.

At this time, Handler has a problem to solve this complex problem. Since Handler runs in the main thread (in the UI thread), it and the child thread can pass data through the Message object. At this time, Handler is responsible for accepting the Message object (the child thread uses the sedMessage() method to pass it over) from the child thread (including data), put these messages into the main thread queue, and cooperates with the main thread to update the UI.

Some features of Handler
The handler can distribute Message objects and Runnable objects to the main thread. Each Handler instance will be bound to the thread that creates it (usually located in the main thread).
It has two functions:

1. Schedule a message or Runnable to execute somewhere in a main thread
2. Arrange an action to execute in different threads
Some ways to distribute messages in Handler

Copy the codeThe code is as follows:

post(Runnable)
postAtTime(Runnable,long)
postDelayed(Runnable long)
sendEmptyMessage(int)
sendMessage(Message)
sendMessageAtTime(Message,long)
sendMessageDelayed(Message,long)

The above post class method allows you to arrange a Runnable object into the main thread queue.
The sendMessage class method allows you to arrange a Message object with data to the queue and wait for updates.


Handler instance
The subclass needs to inherit the Handler class and override the handleMessage(Message msg) method to accept thread data
The following is an example, which implements the function: modify the content of the interface Button through threads

Copy the codeThe code is as follows:

public class MyHandlerActivity extends Activity {
    Button button;
    MyHandler myHandler;

    protected void onCreate(Bundle savedInstanceState) {
        (savedInstanceState);
        setContentView();

        button = (Button) findViewById();
        myHandler = new MyHandler();
// When creating a new Handler instance, it will be bound to the queue of the current thread and message and start distributing data
// Handler has two functions, (1): execute Message and Runnalbe objects regularly
// (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 methods start with post allow you to process Runnable objects
//sendMessage() allows you to process Message objects (Message can contain data,)

        MyThread m = new MyThread();
        new Thread(m).start();
    }

    /**
* Accept messages and process messages, this Handler will run together with the current main thread
    * */

    class MyHandler extends Handler {
        public MyHandler() {
        }

        public MyHandler(Looper L) {
            super(L);
        }

// The subclass must rewrite this method to accept data
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            ("MyHandler", "handleMessage......");
            (msg);
// UI can be updated here
            Bundle b = ();
            String color = ("color");
            (color);

        }
    }

    class MyThread implements Runnable {
        public void run() {

            try {
                (10000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                ();
            }

            ("thread.......", "mThread........");
            Message msg = new Message();
Bundle b = new Bundle();// Store data
("color", "My");
            (b);

(msg); // Send a message to the Handler and update the UI

        }
    }