欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现计时与倒计时的常用方法小结

程序员文章站 2023-12-11 22:06:16
本文实例总结了android实现计时与倒计时的常用方法。分享给大家供大家参考,具体如下: 方法一 timer与timertask(java实现) public...

本文实例总结了android实现计时与倒计时的常用方法。分享给大家供大家参考,具体如下:

方法一

timer与timertask(java实现)

public class timertask extends activity{ 
  private int reclen = 11; 
  private textview txtview; 
  timer timer = new timer(); 
  public void oncreate(bundle savedinstancestate){ 
    super.oncreate(savedinstancestate); 
    setcontentview(r.layout.timertask); 
    txtview = (textview)findviewbyid(r.id.txttime); 
    timer.schedule(task, 1000, 1000);    // timetask 
  }   
  timertask task = new timertask() { 
    @override 
    public void run() { 
      runonuithread(new runnable() {   // ui thread 
        @override 
        public void run() { 
          reclen--; 
          txtview.settext(""+reclen); 
          if(reclen < 0){ 
            timer.cancel(); 
            txtview.setvisibility(view.gone); 
          } 
        } 
      }); 
    } 
  }; 
}

方法二

timertask与handler(不用timer的改进型)

public class timertask extends activity{ 
  private int reclen = 11; 
  private textview txtview; 
  timer timer = new timer(); 
  public void oncreate(bundle savedinstancestate){ 
    super.oncreate(savedinstancestate); 
    setcontentview(r.layout.timertask); 
    txtview = (textview)findviewbyid(r.id.txttime); 
    timer.schedule(task, 1000, 1000);    // timetask 
  }   
  final handler handler = new handler(){ 
    @override 
    public void handlemessage(message msg){ 
      switch (msg.what) { 
      case 1: 
        txtview.settext(""+reclen); 
        if(reclen < 0){ 
          timer.cancel(); 
          txtview.setvisibility(view.gone); 
        } 
      } 
    } 
  }; 
  timertask task = new timertask() { 
    @override 
    public void run() { 
      reclen--; 
      message message = new message(); 
      message.what = 1; 
      handler.sendmessage(message); 
    } 
  }; 
}

方法三

handler与message(不用timertask)

public class timertask extends activity{ 
  private int reclen = 11; 
  private textview txtview; 
  public void oncreate(bundle savedinstancestate) {  
    super.oncreate(savedinstancestate);  
    setcontentview(r.layout.timertask);  
    txtview = (textview)findviewbyid(r.id.txttime); 
    message message = handler.obtainmessage(1);   // message 
    handler.sendmessagedelayed(message, 1000); 
  }  
  final handler handler = new handler(){ 
    public void handlemessage(message msg){     // handle message 
      switch (msg.what) { 
      case 1: 
        reclen--; 
        txtview.settext("" + reclen); 
        if(reclen > 0){ 
          message message = handler.obtainmessage(1); 
          handler.sendmessagedelayed(message, 1000);   // send message 
        }else{ 
          txtview.setvisibility(view.gone); 
        } 
      } 
      super.handlemessage(msg); 
    } 
  }; 
}

方法四

handler与thread(不占用ui线程)

public class timertask extends activity{ 
  private int reclen = 0; 
  private textview txtview; 
  public void oncreate(bundle savedinstancestate){ 
    super.oncreate(savedinstancestate); 
    setcontentview(r.layout.timertask); 
    txtview = (textview)findviewbyid(r.id.txttime); 
    new thread(new mythread()).start();     // start thread 
  }   
  final handler handler = new handler(){     // handle 
    public void handlemessage(message msg){ 
      switch (msg.what) { 
      case 1: 
        reclen++; 
        txtview.settext("" + reclen); 
      } 
      super.handlemessage(msg); 
    } 
  }; 
  public class mythread implements runnable{   // thread 
    @override 
    public void run(){ 
      while(true){ 
        try{ 
          thread.sleep(1000);   // sleep 1000ms 
          message message = new message(); 
          message.what = 1; 
          handler.sendmessage(message); 
        }catch (exception e) { 
        } 
      } 
    } 
  } 
}

方法五

handler与runnable(最简单型)

public class timertask extends activity{ 
  private int reclen = 0; 
  private textview txtview; 
  public void oncreate(bundle savedinstancestate){ 
    super.oncreate(savedinstancestate); 
    setcontentview(r.layout.timertask); 
    txtview = (textview)findviewbyid(r.id.txttime); 
    handler.postdelayed(runnable, 1000); 
  }   
  handler handler = new handler(); 
  runnable runnable = new runnable() { 
    @override 
    public void run() { 
      reclen++; 
      txtview.settext("" + reclen); 
      handler.postdelayed(this, 1000); 
    } 
  }; 
}

计时与倒计时

方法1,方法2和方法3,都是倒计时
方法4,方法5,都是计时
计时和倒计时,都可使用上述方法实现(代码稍加改动)

ui线程比较

方法1,方法2和方法3,都是在ui线程实现的计时;
方法4和方法5,是另开runnable线程实现计时

实现方式比较

方法1,采用的是java实现,即timer和timertask方式;
其它四种方法,都采用了handler消息处理

推荐使用

如果对ui线程交互要求不很高,可以选择方法2和方法3
如果考虑到ui线程阻塞,严重影响到用户体验,推荐使用方法4,另起线程单独用于计时和其它的逻辑处理
方法5,综合了前几种方法的优点,是最简的

希望本文所述对大家android程序设计有所帮助。

上一篇:

下一篇: