Three timer implementation methods for Android

Source: Internet
Author: User

Method 1: Handler + thread

[Java]
View plaincopyprint?
  1. Package com. xunfang. handerdemo;
  2. Import Android. App. activity;
  3. Import Android. OS. Bundle;
  4. Import Android. OS. Handler;
  5. Import Android. OS. message;
  6. Import Android. widget. textview;
  7. /**
  8. * Handler Timer
  9. *
  10. * @ Author smalt
  11. *
  12. */
  13. Public class handerdemoactivity
    Extends activity {
  14. Textview TVshow;
  15. Private int I =
    0;
  16. @ Override
  17. Public void oncreate (bundle savedinstancestate ){
  18. Super. oncreate (savedinstancestate );
  19. Setcontentview (R. layout. Main );
  20. TVshow = (textview) findviewbyid (R. Id. TV _show );
  21. New thread (New threadshow (). Start ();
  22. }
  23. // Handler class receives data
  24. Handler handler = new handler (){
  25. Public void handlemessage (Message MSG ){
  26. If (msg. What = 1 ){
  27. TVshow. settext (integer. tostring (I ++ ));
  28. System. Out. println ("receive ....");
  29. }
  30. };
  31. };
  32. // Thread class
  33. Class threadshow implements runnable {
  34. @ Override
  35. Public void run (){
  36. // Todo auto-generated method stub
  37. While (true ){
  38. Try {
  39. Thread. Sleep (1000 );
  40. Message MSG = new message ();
  41. MSG. What = 1;
  42. Handler. sendmessage (MSG );
  43. System. Out. println ("send ...");
  44. } Catch (exception e ){
  45. // Todo auto-generated Catch Block
  46. E. printstacktrace ();
  47. System. Out. println ("thread error ...");
  48. }
  49. }
  50. }
  51. }
  52. }
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?
  1. Package com. xunfang. handerdemo;
  2. Import Android. App. activity;
  3. Import Android. OS. Bundle;
  4. Import Android. OS. Handler;
  5. Import Android. widget. textview;
  6. /**
  7. * Handler timer is implemented using postdelyed
  8. *
  9. * @ Author smalt
  10. *
  11. */
  12. Public class handerdemoactivity
    Extends activity {
  13. Textview TVshow;
  14. Private int I =
    0;
  15. Private int time =
    1000;
  16. @ Override
  17. Public void oncreate (bundle savedinstancestate ){
  18. Super. oncreate (savedinstancestate );
  19. Setcontentview (R. layout. Main );
  20. TVshow = (textview) findviewbyid (R. Id. TV _show );
  21. Handler. postdelayed (runnable, time); // execute every 1 s
  22. }
  23. Handler handler = new handler ();
  24. Runnable = new runnable (){
  25. @ Override
  26. Public void run (){
  27. // Implement the timer using the handler built-in method
  28. Try {
  29. Handler. postdelayed (this, time );
  30. TVshow. settext (integer. tostring (I ++ ));
  31. System. Out. println ("do ...");
  32. } Catch (exception e ){
  33. // Todo auto-generated Catch Block
  34. E. printstacktrace ();
  35. System. Out. println ("exception ...");
  36. }
  37. }
  38. };
  39. }
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?
  1. Package com. xunfang. handerdemo;
  2. Import java. util. timer;
  3. Import java. util. timertask;
  4. Import Android. App. activity;
  5. Import Android. OS. Bundle;
  6. Import Android. OS. Handler;
  7. Import Android. OS. message;
  8. Import Android. widget. textview;
  9. /**
  10. * Timer implementation: Handler + timer + timertask
  11. *
  12. * @ Author smalt
  13. *
  14. */
  15. Public class handerdemoactivity
    Extends activity {
  16. Textview TVshow;
  17. Private int I =
    0;
  18. Private int time =
    1000;
  19. @ Override
  20. Public void oncreate (bundle savedinstancestate ){
  21. Super. oncreate (savedinstancestate );
  22. Setcontentview (R. layout. Main );
  23. TVshow = (textview) findviewbyid (R. Id. TV _show );
  24. Timer. Schedule (task, 1000,
    1000); // execute the task after 1 s, and then execute the task again after 1 s
  25. }
  26. Handler handler = new handler (){
  27. Public void handlemessage (Message MSG ){
  28. If (msg. What =
    1 ){
  29. TVshow. settext (integer. tostring (I ++ ));
  30. }
  31. Super. handlemessage (MSG );
  32. };
  33. };
  34. Timer timer = new timer ();
  35. Timertask task = new timertask (){
  36. @ Override
  37. Public void run (){
  38. // What needs to be done: Send messages
  39. Message message = new message ();
  40. Message. What = 1;
  41. Handler. sendmessage (Message );
  42. }
  43. };
  44. }
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 );}};}

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.