Method 1: Handler + thread
[Java]
View plaincopyprint?
- Package com. xunfang. handerdemo;
- Import Android. App. activity;
- Import Android. OS. Bundle;
- Import Android. OS. Handler;
- Import Android. OS. message;
- Import Android. widget. textview;
- /**
- * Handler Timer
- *
- * @ Author smalt
- *
- */
- Public class handerdemoactivity
Extends activity {
- Textview TVshow;
- Private int I =
0;
- @ Override
- Public void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- Setcontentview (R. layout. Main );
- TVshow = (textview) findviewbyid (R. Id. TV _show );
- New thread (New threadshow (). Start ();
- }
- // Handler class receives data
- Handler handler = new handler (){
- Public void handlemessage (Message MSG ){
- If (msg. What = 1 ){
- TVshow. settext (integer. tostring (I ++ ));
- System. Out. println ("receive ....");
- }
- };
- };
- // Thread class
- Class threadshow implements runnable {
- @ Override
- Public void run (){
- // Todo auto-generated method stub
- While (true ){
- Try {
- Thread. Sleep (1000 );
- Message MSG = new message ();
- MSG. What = 1;
- Handler. sendmessage (MSG );
- System. Out. println ("send ...");
- } Catch (exception e ){
- // Todo auto-generated Catch Block
- E. printstacktrace ();
- System. Out. println ("thread error ...");
- }
- }
- }
- }
- }
Package COM. xunfang. handerdemo; import android. app. activity; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. widget. textview;/*** handler timer ** @ author smalt **/public class handerdemoactivity extends activity {textview TVshow; private int I = 0; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); TVshow = (textview) findviewbyid (R. id. TV _show); New thread (New threadshow ()). start () ;}// handler class receives data handler = new handler () {public void handlemessage (Message MSG) {If (MSG. what = 1) {TVshow. settext (integer. tostring (I ++); system. out. println ("receive .... ") ;}};}; // Thread class threadshow implements runnable {@ overridepublic void run () {// todo auto-generated method stubwhile (true) {try {thread. sleep (1000); message MSG = new message (); MSG. what = 1; handler. sendmessage (MSG); system. out. println ("send... ");} catch (exception e) {// todo auto-generated catch blocke. printstacktrace (); system. out. println ("thread error... ");}}}}}
Method 2: postdelyed of the handler class
[Java]
View plaincopyprint?
- Package com. xunfang. handerdemo;
- Import Android. App. activity;
- Import Android. OS. Bundle;
- Import Android. OS. Handler;
- Import Android. widget. textview;
- /**
- * Handler timer is implemented using postdelyed
- *
- * @ Author smalt
- *
- */
- Public class handerdemoactivity
Extends activity {
- Textview TVshow;
- Private int I =
0;
- Private int time =
1000;
- @ Override
- Public void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- Setcontentview (R. layout. Main );
- TVshow = (textview) findviewbyid (R. Id. TV _show );
- Handler. postdelayed (runnable, time); // execute every 1 s
- }
- Handler handler = new handler ();
- Runnable = new runnable (){
- @ Override
- Public void run (){
- // Implement the timer using the handler built-in method
- Try {
- Handler. postdelayed (this, time );
- TVshow. settext (integer. tostring (I ++ ));
- System. Out. println ("do ...");
- } Catch (exception e ){
- // Todo auto-generated Catch Block
- E. printstacktrace ();
- System. Out. println ("exception ...");
- }
- }
- };
- }
Package COM. xunfang. handerdemo; import android. app. activity; import android. OS. bundle; import android. OS. handler; import android. widget. textview;/*** handler timer use postdelyed to implement ** @ author smalt **/public class handerdemoactivity extends activity {textview TVshow; private int I = 0; private int time = 1000; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); TVshow = (textview) findviewbyid (R. id. TV _show); handler. postdelayed (runnable, time); // execute every 1 s} handler = new handler (); runnable = new runnable () {@ overridepublic void run () {// Handler comes with the method to implement the timer try {handler. postdelayed (this, time); TVshow. settext (integer. tostring (I ++); system. out. println ("Do... ");} catch (exception e) {// todo auto-generated catch blocke. printstacktrace (); system. out. println ("exception... ");}}};}
Method 3:
Handler + timer + timertask
[Java]
View plaincopyprint?
- Package com. xunfang. handerdemo;
- Import java. util. timer;
- Import java. util. timertask;
- Import Android. App. activity;
- Import Android. OS. Bundle;
- Import Android. OS. Handler;
- Import Android. OS. message;
- Import Android. widget. textview;
- /**
- * Timer implementation: Handler + timer + timertask
- *
- * @ Author smalt
- *
- */
- Public class handerdemoactivity
Extends activity {
- Textview TVshow;
- Private int I =
0;
- Private int time =
1000;
- @ Override
- Public void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- Setcontentview (R. layout. Main );
- TVshow = (textview) findviewbyid (R. Id. TV _show );
- Timer. Schedule (task, 1000,
1000); // execute the task after 1 s, and then execute the task again after 1 s
- }
- Handler handler = new handler (){
- Public void handlemessage (Message MSG ){
- If (msg. What =
1 ){
- TVshow. settext (integer. tostring (I ++ ));
- }
- Super. handlemessage (MSG );
- };
- };
- Timer timer = new timer ();
- Timertask task = new timertask (){
- @ Override
- Public void run (){
- // What needs to be done: Send messages
- Message message = new message ();
- Message. What = 1;
- Handler. sendmessage (Message );
- }
- };
- }
Package COM. xunfang. handerdemo; import Java. util. timer; import Java. util. timertask; import android. app. activity; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. widget. textview;/*** timer implementation: Handler + timer + timertask ** @ author smalt **/public class handerdemoactivity extends activity {textview TVshow; private int I = 0; private int time = 1000; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); TVshow = (textview) findviewbyid (R. id. TV _show); timer. schedule (task, 1000,100 0); // after 1 s, execute the task again after 1 s} handler = new handler () {public void handlemessage (Message MSG) {If (MSG. what = 1) {TVshow. settext (integer. tostring (I ++);} super. handlemessage (MSG) ;};}; timer = new timer (); timertask task = new timertask () {@ overridepublic void run () {// what needs to be done: send message = new message (); message. what = 1; handler. sendmessage (Message );}};}