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

CountDownTimer

企業(yè)建站必須是能夠以充分展現(xiàn)企業(yè)形象為主要目的,是企業(yè)文化與產(chǎn)品對(duì)外擴(kuò)展宣傳的重要窗口,一個(gè)合格的網(wǎng)站不僅僅能為公司帶來巨大的互聯(lián)網(wǎng)上的收集和信息發(fā)布平臺(tái),成都創(chuàng)新互聯(lián)面向各種領(lǐng)域:茶樓設(shè)計(jì)成都網(wǎng)站設(shè)計(jì)全網(wǎng)營銷推廣解決方案、網(wǎng)站設(shè)計(jì)等建站排名服務(wù)。


CountDownTimer 是android 自帶的一個(gè)倒計(jì)時(shí)類,使用這個(gè)類可以很簡單的實(shí)現(xiàn) 倒計(jì)時(shí)功能

CountDownTimer 的實(shí)現(xiàn)方式 

new CountDownTimer(6000,1000) {//第一個(gè)參數(shù)表示的是倒計(jì)時(shí)的總時(shí)間,第二參數(shù)表示的是倒計(jì)時(shí)的間隔時(shí)間。
          @Override
          public void onTick(long millisUntilFinished) {//倒計(jì)時(shí)的過程
            textView.setText(millisUntilFinished / 1000 + "秒");
          }

          @Override
          public void onFinish() {//倒計(jì)時(shí)結(jié)束
            textView.setText("倒計(jì)時(shí)結(jié)束");
          }
        }.start();

實(shí)現(xiàn)效果

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

取消計(jì)時(shí)器

調(diào)用 CountDownTimer 的 cancel() 方法,可以為我們?nèi)∠?jì)時(shí)器:但是這個(gè)方法,只有在 android 5.0 以上才有效果,在android 5.0 以下并沒有效果。如果需要在android 5.0 以下的系統(tǒng)中也使用 cancel,需要我們自己根據(jù) CountDownTimer 源碼中的 實(shí)現(xiàn)方式,重新實(shí)現(xiàn)一下。

  /**
   * Cancel the countdown.
   */
  public synchronized final void cancel() {
    mCancelled = true;
    mHandler.removeMessages(MSG);
  }


 private static final int MSG = 1;


  // handles counting down
  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 5.0以上 增加了一個(gè)

private boolean mCancelled = false;

所以我們只需要在 5.0 以下的系統(tǒng)中,去掉

 if (mCancelled) {
          return;
        }

去掉這個(gè)判斷即可。

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

本文題目:Android中CountDownTimer實(shí)現(xiàn)倒計(jì)時(shí)功能
網(wǎng)頁網(wǎng)址:http://bm7419.com/article0/ijpeio.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)靜態(tài)網(wǎng)站、響應(yīng)式網(wǎng)站網(wǎng)站維護(hù)、網(wǎng)站導(dǎo)航、營銷型網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)