Android-源码剖析CountDownTimer(倒计时类)
生活随笔
收集整理的這篇文章主要介紹了
Android-源码剖析CountDownTimer(倒计时类)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
簡介
CounterDownTImer是Android系統自帶的一個倒計時器,特別是在做app登錄時會比較有用。
用法
非常簡單,比如做個倒計時60s且每隔1s會刷新一下,可以這樣寫
new CountDownTimer(60000, 1000) {public void onTick(long millisLeft) {mTextField.setText("seconds millisLeft: " + millisLeft / 1000);}public void onFinish() {mTextField.setText("done!");}}.start();CountDownTimer類的構造器接受2個參數,第一個為倒計時總時間,第二個為間隔時間單位。注意2個參數單位都是ms。當調用start方法后,系統會每間隔一段時間(第二個參數指定)調用onTrick方法,倒計時結束會執行onFinish方法;那么到底是怎么實現的呢?
下面是源碼以及對源碼理解所做的注釋,就不再解釋了
package android.os; /*** The calls to {@link #onTick(long)} are synchronized to this object so that* one call to {@link #onTick(long)} won't ever occur before the previous* callback is complete. This is only relevant when the implementation of* {@link #onTick(long)} takes an amount of time to execute that is significant* compared to the countdown interval.*/ public abstract class CountDownTimer {/*** Millis since epoch when alarm should stop.*/private final long mMillisInFuture;//倒計時總耗時間(單位:ms)/*** The interval in millis that the user receives callbacks*/private final long mCountdownInterval;//間隔時間(單位:ms)private long mStopTimeInFuture;//倒計時結束時間戳(單位:ms)/*** boolean representing if the timer was cancelled*/private boolean mCancelled = false;/*** @param millisInFuture The number of millis in the future from the call* to {@link #start()} until the countdown is done and {@link #onFinish()}* is called.* @param countDownInterval The interval along the way to receive* {@link #onTick(long)} callbacks.*/public CountDownTimer(long millisInFuture, long countDownInterval) {mMillisInFuture = millisInFuture;mCountdownInterval = countDownInterval;}/*** Cancel the countdown.*/public synchronized final void cancel() {mCancelled = true;mHandler.removeMessages(MSG);}/*** Start the countdown.*/public synchronized final CountDownTimer start() {mCancelled = false;//判斷構造器接受的參數是否合法,//如果倒計時總耗時間<=0 直接調用onFinish方法,并立即返回該對象if (mMillisInFuture <= 0) {onFinish();return this;}//保存倒計時結束時間戳mStopTimeInFuture = SystemClock.elapsedRealtime() + mMillisInFuture;//發送消息通知mHandler.sendMessage(mHandler.obtainMessage(MSG));return this;}/*** Callback fired on regular interval.* @param millisUntilFinished The amount of time until finished.*/public abstract void onTick(long millisUntilFinished);/*** Callback fired when the time is up.*/public abstract void onFinish();private static final int MSG = 1;//注意此處mHandler成員變量的構造器沒有傳入Looper哦// handles counting downprivate Handler mHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {synchronized (CountDownTimer.this) {if (mCancelled) {return;}//計算還剩下多少時間需要倒計時final long millisLeft = mStopTimeInFuture - SystemClock.elapsedRealtime();if (millisLeft <= 0) {onFinish();} else if (millisLeft < mCountdownInterval) {//如果還剩的時間比間隔時間還小,不再調用onTick方法,延遲剩余的時間發送通知后直接執行onFinish方法// no tick, just delay until donesendMessageDelayed(obtainMessage(MSG), millisLeft);} else {long lastTickStart = SystemClock.elapsedRealtime();onTick(millisLeft);/***delay = 間隔時間-onTick消耗的時間*如果delay >=0 ,為保證onTick是嚴格間隔時間執行,延遲delay發送通知*如果delay < 0, 說明執行onTick方法消耗的時間比間隔時間大,只能跳過當前時序,直接進入下一個時序**/// take into account user's onTick taking time to executelong delay = lastTickStart + mCountdownInterval - SystemClock.elapsedRealtime();// special case: user's onTick took more than interval to// complete, skip to next intervalwhile (delay < 0) delay += mCountdownInterval;sendMessageDelayed(obtainMessage(MSG), delay);}}}}; }總結:
總結
以上是生活随笔為你收集整理的Android-源码剖析CountDownTimer(倒计时类)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android秒级编译方案-FreeLi
- 下一篇: Android 跨进程通信: AIDL