使用介绍

开发中经常会遇到一些和倒计时有关的场景,比如发送验证码的按钮,会在点击发送后,显示倒计时间,倒计时结束后才能够刷新按钮,再次允许点击。为了不阻塞软件的运行,又要实时刷新界面,我们通常会用到 Handler 或者 AsyncTask 等技术,自己写逻辑实现。其实 Android 中已经封装好了一套 CountDownTimer 来实现这个功能需求。

CountDownTimer(long millisInFuture, long countDownInterval)

CountDownTimer的两个参数分别表示倒计时的总时间 millisInFuture 和间隔时间 countDownInterval。

具体的调用如下:

TextView vertifyBtn;
CountDownTimer timer = new CountDownTimer(60000, 1000) { @Override
public void onTick(long millisUntilFinished) {
vertifyBtn.setText((millisUntilFinished / 1000) + " second");
} @Override
public void onFinish() {
vertifyBtn.setEnabled(true);
vertifyBtn.setText("Send");
}
};
timer.start();

上面的调用举例表示总计 60 秒,每 1 秒都会执行一次 onTick 方法,其参数 millisUntilFinished 表示倒计时剩余时间毫秒数,最后倒计时结束执行 onFinish 方法。

实现原理

下面是 CountDownTimer 的源码,代码非常少,很好理解。从源代码中可以看出,其实 CountDownTimer 也是利用 Handler 的消息处理机制来实现效果的。初始化设定好起始和终止时间后,每隔一定的间隔时间通过 Handler 给主线程发送消息,然后再在消息处理中回调方法。好好利用官方封装好的工具类,可以避免我们重复的造轮子,当然了解轮子的原理就更好了!

package android.os;

public abstract class CountDownTimer {
private final long mMillisInFuture;
private final long mCountdownInterval;
private long mStopTimeInFuture;
private boolean mCancelled = false; public CountDownTimer(long millisInFuture, long countDownInterval) {
mMillisInFuture = millisInFuture;
mCountdownInterval = countDownInterval;
} public synchronized final void cancel() {
mCancelled = true;
mHandler.removeMessages(MSG);
} public synchronized final CountDownTimer start() {
mCancelled = false;
if (mMillisInFuture <= 0) {
onFinish();
return this;
}
mStopTimeInFuture = SystemClock.elapsedRealtime() + mMillisInFuture;
mHandler.sendMessage(mHandler.obtainMessage(MSG));
return this;
} public abstract void onTick(long millisUntilFinished); public abstract void onFinish(); private static final int MSG = 1; private Handler mHandler = new Handler() { @Override
public void handleMessage(Message msg) { synchronized (CountDownTimer.this) {
if (mCancelled) {
return;
} final long millisLeft = mStopTimeInFuture - SystemClock.elapsedRealtime(); if (millisLeft <= 0) {
onFinish();
} else if (millisLeft < mCountdownInterval) {
// no tick, just delay until done
sendMessageDelayed(obtainMessage(MSG), millisLeft);
} else {
long lastTickStart = SystemClock.elapsedRealtime();
onTick(millisLeft); // take into account user's onTick taking time to execute
long delay = lastTickStart + mCountdownInterval - SystemClock.elapsedRealtime(); // special case: user's onTick took more than interval to
// complete, skip to next interval
while (delay < 0) delay += mCountdownInterval; sendMessageDelayed(obtainMessage(MSG), delay);
}
}
}
};
}
05-27 18:14