If you need a time-consuming operation in the activity, such as when we need to read data online or read a larger local file, we cannot put these operations in the main thread. Because if placed in the main thread, the interface will appear suspended animation, if 5 seconds has not been completed, you will receive an Android system error prompted "forced shutdown." This time we need to put these time-consuming operations in a child thread, because the child thread involves UI updates, the Android main thread is not safe, that is, the update UI can only be updated in the main thread, the child thread operation is dangerous. This time, handler appeared, to solve this complex problem. Handler mainly accepts data sent by child threads and uses this data to update the UI with the main thread. , because Handler is running in the mainline thread (UI thread), it and child threads can pass data through the message object, at which point Handler takes over the message object passed by the child thread (the child thread passes the Sedmessage () method) (the Surface contains data), put these messages into the main thread queue and update the UI with the main thread. Handler is an invisible control that runs in an activity, including a queue container that can hold the main thread's child threads, and a message queue container for child and main thread traffic. Handler can load thread objects into a thread queue, or you can remove thread execution from the thread queue.
 
Basic usage of handler:
 
Post (Runnable) adds a thread object to the queue
 
Postattime (Runnable,long) executes Runnable object at a specified time (uptimemillis)
 
Postdelayed (Runnable,long) executes the Runnable object after a specified interval (delaymillis)
 
The general steps used by handler are:
 
1. First declare a handler object in the activity.
 
2. Define one or more thread objects, copy the Run () method of the Thread object, and write the action you want to perform in the Run () method.
 
3. The handler post () (post () has multiple overloaded forms) method thread object is loaded into the handler thread queue. The system will remove the team head thread from the current thread queue and execute it at the appropriate time.
 
4. When you need to stop the execution of a thread in the thread queue, you can call the Removecallbacks () method in the main thread to delete the thread object that is not executing in the queue.
 
Example: Create a new Android application project, Main.xml always add two button buttons. The process of the program is when we click the Start button in the activity, the handler object in the activity loads a thread object updatethread into the thread queue, in which the "Updatethread" is printed on the console, And the thread is loaded into the handler thread queue again to form a circular call. This allows you to keep outputting text content. When we click the End button, we call the Removecallbacks () method, delete the thread object, stop the execution of the thread, and no longer print the text content.
 
Main.xml
 
01.<?xml version="1.0" encoding="utf-8"? >
 02.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"03. android:orientation="vertical"
 04. android:layout_width="fill_parent"
 05. android:layout_height="fill_parent"
 06. >
 07.<TextView
 08. android:layout_width="fill_parent"
 09. android:layout_height="wrap_content"
 10. android:text="@string/hello"
 11. />
 12.<Button
 13. android:id="@+id/startButton"
 14. android:layout_width="300dp"
 15. android:layout_height="wrap_content"
 16. android:text="start"
 17. />
 18.<Button
 19. android:id="@+id/endButton"
 20. android:layout_width="300dp"
 21. android:layout_height="wrap_content"
 22. android:text="end"
 23. />
 24.</LinearLayout>