Package com. example. testhandler; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. app. activity; // Handler common methods: // 1 post () // 2 postDelayed () // 3 sendMessage () // 4 handleMessage () public class MainActivity extends Activity {Runnable updateRunnable; private final int CANCEL_RUNNABLE = 99; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); final Handler handler = new Handler () {@ Override public void handleMessage (Message msg) {super. handleMessage (msg); // The first kind of Handler is as follows: handle if (msg. what = 1) {System. out. println ("Receive Message");} // the first type is Handler. Handle the Message according to what it is. // The second type is Handler. postDelayed (Runnable, delayMillis) if (msg. what = CANCEL_RUNNABLE) {System. out. println ("current message msg. what = "+ msg. what); // cancel Runnable execution removeCallbacks (updateRunnable);} // The preceding code is Handler. The second is handler. postDelayed (Runnable, delayMillis )}}; //////////////////////////////////////// //////////////////////////////////////// //// // The following is: handler type 1: Send a Message new Thread () {@ Override public void run () {Message message1 = new Message (); message1.what = 1; message1.obj = new Object (); // handle sends out a Message object // to facilitate the test to annotate the sentence // handler. sendMessage (message1); super. run () ;}}; // the first one is Handler. // the second one is Handler. postDelayed (Runnable, delayMillis) // The issued Runnable will be delayed for a certain period of time in the thread bound to the Handler to execute updateRunnable = new Runnable () {public void run () {for (int I = 5; I <15; I ++) {System. out. println ("I =" + I) ;}}; // handle sends out a Runnable object // to facilitate the test, comment the following two sentences // handler cannot be used. post (updateRunnable); // handler should be used. postDelayed (updateRunnable, 5000); // then immediately sends a message, so that you can call the removeCallbacks () method handler in handleMessage. postDelayed (updateRunnable, 1000); handler. sendEmptyMessage (CANCEL_RUNNABLE); // The second is Handler. postDelayed (Runnable, delayMillis) // The third type is Handler. post (Runnable) // The issued Runnable will be executed by the neutral horse in the thread bound to Handler // to facilitate the test, comment the following code sentence // handler. post (updateRunnable); // The third type is Handler. post (Runnable )}}