验证码倒计时

上次写过系统提供的验证码倒计时的一个类 CountDownTimer 但是这个类在使用的时候总是会出现倒计时的偏差,所以只好自##### 己使用 Handler 或者 Timer 来实现。项目中 注册、忘记密码还##### 有现在有出现一个直接拿手机号码配合验证码直接登陆的都用到了验证码倒计时,这是后我们不可能每个类里面都使用 Handler,最直接的就是抽取出来一个,也减少了代码的重复性。
  • 我就跟着系统的 CountDownTimer 直接仿了一个来重复使用。

  • 示例代码

public abstract class CountDownTimer {
    private static final String TAG = "CountDownTimer";
    /**
     * 倒计时间
     */
    private int millisInFuture;
    private int mCountTimer;
    /**
     * 倒计时间隔时间
     */
    private final int mCountdownInterval;
    private static final int MSG = 1;
    /**
     * boolean representing if the timer was cancelled
     */
    private boolean mCancelled = false;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG:
                    synchronized (CountDownTimer.this) {
                        if (mCancelled) {
                            return;
                        }
                        if (millisInFuture <= 0) {
                            millisInFuture = mCountTimer;
                            onFinish();
                        } else {
                            onTick(millisInFuture);
                            millisInFuture--;
                            mHandler.sendEmptyMessageDelayed(MSG, mCountdownInterval);
                        }
                    }
                    break;
            }
        }
    };

    /**
     * Cancel the countdown.
     */
    public synchronized final void cancel() {
        mCancelled = true;
        if (mHandler.hasMessages(MSG)|mHandler!=null) {
            mHandler.removeMessages(MSG);
            onFinish();
        }


    }

    public CountDownTimer(int millisInFuture, int mCountdownInterval) {
        this.millisInFuture = millisInFuture;
        this.mCountdownInterval = mCountdownInterval;
        this.mCountTimer = millisInFuture;
    }

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

    /**
     * Start the countdown.
     */
    public synchronized final CountDownTimer start() {
        mCancelled = false;
        Log.e(TAG, "start: " + millisInFuture);
        if (millisInFuture <= 0) {
            onFinish();
            return this;
        }
        Log.e(TAG, "start1: " + millisInFuture);
        mHandler.sendEmptyMessage(MSG);
        return this;
    }
}
  • 上面的直接复制到项目里面就可以重复使用了,代码是从项目里直接贴的这里就不上传代码和效果图了。
如果要是类名不更改记得在使用的过程中药导入自己的包名而不是系统的 的包名。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值