Handler common methods in Android

Source: Internet
Author: User
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 )}}

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.