Android中四种计时器Thread、Timer、CountDownTimer、handler.postDelayed的使用

From: http://blog.csdn.net/u010687392/article/details/43955395

 

Android开发中,我们常常需要用到计时器,倒计时多少秒后再执行相应的功能,下面我就分别来讲讲这三种常用的计时的方法。

一、CountDownTimer

该类是个抽象类,如果要使用这个类中的方法,就必须继承这个类实现它内部的抽象方法,该继承的类中通常是存在某个类的内部类中。该类主要功能就是可以我们自己设置倒计时的秒数和时间间隔,且只有一个构造方法,构造函数为:

 

[java]  view plain  copy
 
 print?
  1. public CountDownTimer(long millisInFuture, long countDownInterval) {//分别为总时间和时间间隔  
  2.        mMillisInFuture = millisInFuture;  
  3.        mCountdownInterval = countDownInterval;  
  4.    }  

可以看出需要传入两个参数进去,第一个参数就是我们设置倒计时的总时间,第二个参数就是我们设置倒计时时的时间间隔。

 

我们来使用一下:

 

[java]  view plain  copy
 
 print?
  1. class TimeCountDown extends CountDownTimer{  
  2.   
  3.     public TimeCountDown(long millisInFuture, long countDownInterval) {  
  4.         super(millisInFuture, countDownInterval);  
  5.         // TODO Auto-generated constructor stub  
  6.     }  
  7.   
  8.     @Override  
  9.     public void onTick(long millisUntilFinished) {  
  10.         // TODO Auto-generated method stub  
  11.           
  12.     }  
  13.   
  14.     @Override  
  15.     public void onFinish() {  
  16.         // TODO Auto-generated method stub  
  17.           
  18.     }  
  19.       
  20. }  

可以看到,子类必须要和它的构造函数参数相同之外,还必须实现它的两个方法,分别是:onTick()和onFinish()方法,

 

我们说说这两个方法分别是什么意思:

onTick(long millisUntilFinished):这个方法中的参数是在倒计时过程中传入进来的毫秒数,比如倒计时总时间为6秒,时间间隔为一秒,那么这个参数的传进来的值依次为:5、4、3、2、1

onFinish():这个方法就是当倒计时完毕时候触发,这时候可以设置一些配置,比如一个申请验证码的按钮,在倒计时过程中设置为失去焦点,等倒计时完毕后,就在这个方法中设置为得到焦点。

【注意】使用时也非常简单,只需要new个对象出来并传入总时间和时间间隔(单位为毫秒),这时候千万别漏了.start()方法啊,让它启动,因为作者就有次漏了,找半天发现是没写start。

下面通过部分代码来看一下整个的实现流程:

 

[java]  view plain  copy
 
 print?
  1. class TimeCount extends CountDownTimer {  
  2.         public TimeCount(long millisInFuture, long countDownInterval) {  
  3.             super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔  
  4.         }  
  5.   
  6.         @Override  
  7.         public void onFinish() {// 计时完毕时触发  
  8.             mButtonConfirmCode.setText("重新获取");  
  9.             mButtonConfirmCode.setClickable(true);  
  10.             mButtonConfirmCode.setBackgroundColor(Color.rgb(187, 201, 230));  
  11.         }  
  12.   
  13.         @Override  
  14.         public void onTick(long millisUntilFinished) {// 计时过程显示  
  15.             mButtonConfirmCode.setClickable(false);  
  16.             mButtonConfirmCode.setBackgroundColor(Color.GRAY);  
  17.             mButtonConfirmCode.setText("重新获取(" + millisUntilFinished / 1000  
  18.                     + ")秒后");  
  19.         }  
  20.     }  

使用时这么使用:

 

new TimeCount(60000, 1000).start();

注意要start();

 

二、Timer

这个类功能也是用于计时的,而且是线程安全的,创建这个类的对象也很简单,直接Timer mTimer=new Timer();new出来,也没什么可说的,而且这个对象的主要方法就一个就是schedule(),意思就是计时计划,计时安排。我就主要讲其中两个方法吧:

mTimer.schedule(task, when);这个方法第一个参数就是需要传入一个TimerTask对象,该对象实现了Runnable接口,简单的说就是一个线程对象,就是开一个线程来执行run()方法中的代码的。第二个参数传入的类型是Date类型,意思就是定时一样,这个run()方法中的代码在什么时候执行。
mTimer.schedule(task, delay);这个方法的第一个参数和上面的一样。第二个参数就是一个long类型的数据,意思就是延迟多少时间执行run()方法中的代码,单位为毫秒。

我就讲直接上代码看看怎么用吧:

[java]  view plain  copy
 
 print?
  1. Timer mTimer = new Timer();  
  2. TimerTask mTimerTask = new TimerTask() {//创建一个线程来执行run方法中的代码  
  3.     @Override  
  4.     public void run() {  
  5.     //要执行的代码  
  6.     }  
  7. };  
  8. mTimer.schedule(mTimerTask, 3000);//延迟3秒执行  

【注意】在该计时器中创建的线程不是主线程,而是创建了一个子线程,因为子线程中不能直接更新UI,故在run方法中不能执行有关更新主界面UI的代码,如果要更新UI,那必须用消息处理器Handler来处理,在run中发送一个消息给消息队列。

 

 

如果我们想既要达到计时的功能,又可以在执行的代码中直接更新主界面UI,那有没有一个好办法呢?答案是有的,就是第三点

三、new Handler().postDelayed()

该方法就是利用我们常说的消息处理器。该方法原理就是在主线程中创建一个Handler消息处理器,然后利用其中的一个postDelayed(Runnable r, long delayMillis)方法,该方法第一个参数需要传入一个Runnable接口,并实现run()方法,第二个参数就是延迟多少时间将run()方法中的代码通过一个消息发送给消息队列,然后在主线程中执行这个消息中的代码,即是run方法中的代码,从而实现在主线程中更新界面UI。

贴代码吧:

 

[java]  view plain  copy
 
 print?
  1. new Handler().postDelayed(new Runnable() {//在当前线程(也即主线程中)开启一个消息处理器,并在3秒后在主线程中执行,从而来更新UI  
  2.     @Override  
  3.     public void run() {  
  4.         //有关更新UI的代码  
  5.     }  
  6. }, 3000);//3秒后发送          

 

四、直接使用Thread


Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
xxxx
} catch (InterruptedException e) {

}
}
});
thread.start();

转载于:https://www.cnblogs.com/fuyoucaoyu/articles/5834056.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值