亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android CountDownTimer實(shí)現(xiàn)倒計(jì)時(shí)器

 更新時(shí)間:2017年02月25日 08:48:13   作者:銀色子彈  
這篇文章主要為大家詳細(xì)介紹了Android CountDownTimer實(shí)現(xiàn)倒計(jì)時(shí)效果的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

使用介紹

開發(fā)中經(jīng)常會(huì)遇到一些和倒計(jì)時(shí)有關(guān)的場景,比如發(fā)送驗(yàn)證碼的按鈕,會(huì)在點(diǎn)擊發(fā)送后,顯示倒計(jì)時(shí)間,倒計(jì)時(shí)結(jié)束后才能夠刷新按鈕,再次允許點(diǎn)擊。為了不阻塞軟件的運(yùn)行,又要實(shí)時(shí)刷新界面,我們通常會(huì)用到 Handler 或者 AsyncTask 等技術(shù),自己寫邏輯實(shí)現(xiàn)。其實(shí) Android 中已經(jīng)封裝好了一套 CountDownTimer 來實(shí)現(xiàn)這個(gè)功能需求。

CountDownTimer(long millisInFuture, long countDownInterval)

CountDownTimer的兩個(gè)參數(shù)分別表示倒計(jì)時(shí)的總時(shí)間 millisInFuture 和間隔時(shí)間 countDownInterval。

具體的調(diào)用如下:

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

上面的調(diào)用舉例表示總計(jì) 60 秒,每 1 秒都會(huì)執(zhí)行一次 onTick 方法,其參數(shù) millisUntilFinished 表示倒計(jì)時(shí)剩余時(shí)間毫秒數(shù),最后倒計(jì)時(shí)結(jié)束執(zhí)行 onFinish 方法。

實(shí)現(xiàn)原理

下面是 CountDownTimer 的源碼,代碼非常少,很好理解。從源代碼中可以看出,其實(shí) CountDownTimer 也是利用 Handler 的消息處理機(jī)制來實(shí)現(xiàn)效果的。初始化設(shè)定好起始和終止時(shí)間后,每隔一定的間隔時(shí)間通過 Handler 給主線程發(fā)送消息,然后再在消息處理中回調(diào)方法。好好利用官方封裝好的工具類,可以避免我們重復(fù)的造輪子,當(dāng)然了解輪子的原理就更好了!

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

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論