广告
返回顶部
首页 > 资讯 > 移动开发 >Android CountDownTimer实现倒计时器
  • 356
分享到

Android CountDownTimer实现倒计时器

倒计时计时器Android 2022-06-06 04:06:01 356人浏览 八月长安
摘要

使用介绍 开发中经常会遇到一些和倒计时有关的场景,比如发送验证码的按钮,会在点击发送后,显示倒计时间,倒计时结束后才能够刷新按钮,再次允许点击。为了不阻塞软件的运行,又要实时

使用介绍

开发中经常会遇到一些和倒计时有关的场景,比如发送验证码的按钮,会在点击发送后,显示倒计时间,倒计时结束后才能够刷新按钮,再次允许点击。为了不阻塞软件的运行,又要实时刷新界面,我们通常会用到 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)Android利用CountDownTimer实现倒计时功能 Android实现停留5s跳转到登录页面Android中CountDownTimer 实现倒计时功能Android中使用Handler及Countdowntimer实现包含倒计时的闪屏页面Android利用CountDownTimer实现点击获取验证码倒计时效果Android使用CountDownTimer实现倒计时效果Android基于CountDownTimer实现倒计时功能Android中CountDownTimer倒计时器用法实例Android使用CountDownTimer模拟短信验证倒计时


--结束END--

本文标题: Android CountDownTimer实现倒计时器

本文链接: https://www.lsjlt.com/news/22176.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作