CountDownTimer

Schedule a countdown until a time in the future, with regular notifications on intervals along the way. Example of showing a 30 second countdown in a text field:

new CountDownTimer(30000, 1000) {

     public void onTick(long millisUntilFinished) {
         mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
     }

     public void onFinish() {
         mTextField.setText("done!");
     }
  }.start();

The calls to onTick(long) are synchronized to this object so that one call to onTick(long) won't ever occur before the previous callback is complete. This is only relevant when the implementation of onTick(long) takes an amount of time to execute that is significant compared to the countdown interval.

Public constructors

CountDownTimer (long millisInFuture, long countDownInterval)
millisInFuture----long: The number of millis in the future from the call to start() until 
the countdown is done and onFinish() is called. countDownInterval----long: The interval along the way to receive onTick(long) callbacks.

Public methods

(1)cancel----void cancel ()

Cancel the countdown.

(2)onFinish----void onFinish ()

Callback fired when the time is up.

(3)onTick----void onTick (long millisUntilFinished)

Callback fired on regular interval.

(4)start----CountDownTimer start ()

Start the countdown.

 

理解:

CountDownTimer----android倒计时方法

从官方文档的代码可以看出CountDownTimer每隔1秒调用一次onTick(long millisUntilFinished)方法,倒计时介绍时调用onFinish()方法.

  • 方法1----cancel(): 取消当前的任务
  • 方法2----onFinish(): 当前任务完成的时候回调
  • 方法3----onTick(long millisUntilFinished): 当前任务每完成一次倒计时间隔时间时回调
  • 方法4----start(): 开始当前的任务

从CountDownTimer的源码可以知道,他并不是一个完整的计时器,是通过handler实现倒计时功能的。



 

posted on 2017-03-14 10:36  不二家的瓶子  阅读(151)  评论(0编辑  收藏  举报

导航