使用介绍
开发中经常会遇到一些和倒计时有关的场景,比如发送验证码的按钮,会在点击发送后,显示倒计时间,倒计时结束后才能够刷新按钮,再次允许点击。为了不阻塞软件的运行,又要实时刷新界面,我们通常会用到 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); } } } };}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android CountDownTimer实现倒计时器全部内容,希望文章能够帮你解决Android CountDownTimer实现倒计时器所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)