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 Handler type is as follows: handle the Message according to what is different.
If (msg. what = 1 ){
System. out. println ("Receive message ");
}
// The first type is Handler: handle the Message according to what it is.

// Handler: handler. postDelayed (Runnable, delayMillis)
If (msg. what = CANCEL_RUNNABLE ){
System. out. println ("current message msg. what =" + msg. what );
// Cancel Runnable execution
RemoveCallbacks (updateRunnable );
}
// The second type is Handler. postDelayed (Runnable, delayMillis)
}
};

//////////////////////////////////////// //////////////////////////////////////// /////////////

// The following is the first type of Handler: Send a Message
New Thread (){
@ Override
Public void run (){
Message message1 = new Message ();
Message1.what = 1;
Message1.obj = new Object ();
// Handle sends a Message object
// To facilitate the test to annotate the sentence
// Handler. sendMessage (message1 );
Super. run ();
}
};
// The preceding example is Handler. The first example is to send a Message.

// Handler: handler. postDelayed (Runnable, delayMillis)
// The Runnable will be delayed for a certain period of execution in the thread bound to the Handler.
UpdateRunnable = new Runnable (){
Public void run (){
For (int I = 5; I <15; I ++ ){
System. out. println ("I =" + I) in Runnable );
}
}
};
// Handle sends out a Runnable object
// Comments the following two sentences to facilitate the test
// Handler. post (updateRunnable) cannot be used );
// Handler. postDelayed (updateRunnable, 5000) should be used );
// Then a message is sent immediately, so that it can be in handleMessage
// Call the removeCallbacks () method
Handler. postDelayed (updateRunnable, 1000 );
Handler. sendEmptyMessage (CANCEL_RUNNABLE );
// The second type is Handler. postDelayed (Runnable, delayMillis)


// Handler. post (Runnable)
// The issued Runnable will be executed by a neutral horse in the thread bound to the Handler.
// Comments the following code to facilitate the test
// Handler. post (updateRunnable );
// The third type is Handler. post (Runnable)
}

 

}

Related Article

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.