Android Three implementation of the timer detailed and implementation methods _android

Source: Internet
Author: User

method One: Handler+thread

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 (); The//Handler class receives data Handler Handler = new Handler () {public void Handlemessage (msg) {if (Msg.what = = 
    1) {Tvshow.settext (integer.tostring (i++)); 
   SYSTEM.OUT.PRINTLN ("Receive ..."); 
 } 
  }; 
 
 }; Thread Classes Class Threadshow implements Runnable {@Override public void run () {//TODO auto-generated Method St 
     UB 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 ..."); 

 } 
   } 
  } 
 } 
}

Method Two: Handler class postdelyed

Package Com.xunfang.handerDemo; 
Import android.app.Activity; 
Import Android.os.Bundle; 
Import Android.os.Handler; 
 
Import Android.widget.TextView; /** * Handler timer using postdelyed implementation * * @author Smalt * */public class Handerdemoactivity extends activity {Text 
 View 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); 
 every 1s execution} Handler Handler = new Handler (); Runnable Runnable = new Runnable () {@Override public void run () {//handler own method implementation timer 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 ..."); 
 
} 
  } 
 };} 
 

method Three: Handler+timer+timertask

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); 1s execute task after 1s execute again} Handler Handler = new Handler () {public void Handlemessage (msg) {if (MSG.WH 
   at = = 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 a message 
   Message message = new Message (); 
   Message.what = 1; 
  Handler.sendmessage (message); 
} 
 }; 
 }

Above is on the Android timer data collation follow-up to continue to supplement the relevant knowledge, thank you for your support to this site!

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.