Android中CountDownTimer類詳解

一、概述

項目中經常用到倒計時的功能,比如說限時搶購,手機獲取驗證碼等等。而google官方也幫我們封裝好瞭一個類:CountDownTimer,使我們的開發更加方便;

二、API

CountDownTimer是一個抽象類,有兩個抽象方法,它的API很簡單

public abstract void onTick(long millisUntilFinished);//這個是每次間隔指定時間的回調,millisUntilFinished:剩餘的時間,單位毫秒
public abstract void onFinish();//這個是倒計時結束的回調

使用的時候隻需要

new CountDownTimer(long millisInFuture, long countDownInterval)
//millisInFuture:倒計時的總時長
//countDownInterval:每次的間隔時間  單位都是毫秒

三、基本使用方法

我們以短信驗證碼的倒計時來看,點擊獲取驗證碼,倒計時60s不可點擊

new CountDownTimer(60 * 1000, 1000) {
    @Override
    public void onFinish() {
        if (tvCode != null) {
            tvCode.setText("重新獲取");
            tvCodeWr.setTextColor(Color.parseColor("#E94715"));
            tvCode.setClickable(true);
            tvCode.setEnabled(true);
        }

        cancel();
    }

    @Override
    public void onTick(long millisUntilFinished) {
        if (tvCode != null) {
            tvCode.setClickable(false);
            tvCode.setEnabled(false);
            tvCode.setText(millisUntilFinished / 1000 + "s");
            tvCode.setTextColor(Color.parseColor("#999999"));
        }
    }
}.start();

點擊按鈕,獲取驗證碼成功之後就可以執行以上操作,最後一定要start,不然不會執行

四、使用註意

CountDownTimer使用很簡單,但是坑很多,需要註意避免踩坑。

1、空指針:

如果在activity或者fragment關閉銷毀的時候沒有調用cancle方法,它的onTick方法還是會繼續執行,這個時候UI控件都為空,不註意判斷的話很容易空指針

2、時間不是太準的問題:

我們看CountDownTimer的源碼可以看到,在執行onTick的方法時,google源碼裡面減去瞭程序執行到這裡的時候所消耗的時間,這裡可以看出google代碼的嚴謹

final long millisLeft = mStopTimeInFuture - SystemClock.elapsedRealtime();

if (millisLeft <= 0) {
    onFinish();
} else if (millisLeft < mCountdownInterval) {
    // no tick, just delay until done
    sendMessageDelayed(obtainMessage(MSG), millisLeft);
}

所以一開始倒計時的時間是59,這裡可以在構造方法裡面稍微加一點時間就可以解決如:

new CountDownTimer(60 * 1000+300, 1000)

3、內存泄漏問題

首先我們來看源碼,核心代碼如下

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);
            }
        }
    }
};

可以看到CountDownTimer的原理還是用到瞭Handler,所以很容易造成內存泄漏問題,當Activity或者Fragment關閉而倒計時還未結束的時候,會在後臺一直執行,而很多時候我們用倒計時會有更新UI的操作,而控件都持有activity的引用,長期得不到釋放的話就會造成內存泄漏,甚至會造成1所說的空指針問題,所以一般要在activity或fragment銷毀的時候調用cancle方法。

我自己把這個進行瞭封裝,寫成瞭一個工具類以供參考:

public class TimeUtils {
    private String color;//這裡可以修改文字顏色
    WeakReference<TextView> tvCodeWr;//控件軟引用,防止內存泄漏
    private CountDownTimer timer;


    public TimeUtils(TextView tvCode, String color) {
        super();
        this.tvCodeWr = new WeakReference(tvCode);
        this.color = color;
    }
//這是倒計時執行方法
    public void RunTimer() {
        timer = new CountDownTimer(60 * 1000 - 1, 1000) {
            @Override
            public void onFinish() {
                if (tvCodeWr.get() != null) {
                    tvCodeWr.get().setText("重新獲取");
                    tvCodeWr.get().setTextColor(Color.parseColor(color));
                    tvCodeWr.get().setClickable(true);
                    tvCodeWr.get().setEnabled(true);
                }

                cancel();
            }

            @Override
            public void onTick(long millisUntilFinished) {
                if (tvCodeWr.get() != null) {
                    tvCodeWr.get().setClickable(false);
                    tvCodeWr.get().setEnabled(false);
                    tvCodeWr.get().setText(millisUntilFinished / 1000 + "s");
                    tvCodeWr.get().setTextColor(Color.parseColor("#999999"));
                }
            }
        }.start();
    }
//這個方法可以在activity或者fragment銷毀的時候調用,防止內存泄漏
    public void cancle() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }
}

到此這篇關於Android中CountDownTimer類詳解的文章就介紹到這瞭。希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: