倒计时

2019-09-26  本文已影响0人  Dream_Book

CountDownTimer

private void countDown(long millisInFuture) {
        if (mTimer != null) {
            mTimer.cancel();//因为内部使用的是handler不合理消除容易造成内存泄漏
        }
        mTimer = new CountDownTimer(millisInFuture, 1000) {
            @Override//剩余时间
            public void onTick(long millisUntilFinished) {
                long day = millisUntilFinished / (1000 * 24 * 60 * 60); //单位天
                long hour = (millisUntilFinished - day * (1000 * 24 * 60 * 60)) / (1000 * 60 * 60); //单位时
                long minute = (millisUntilFinished - day * (1000 * 24 * 60 * 60) - hour * (1000 * 60 * 60)) / (1000 * 60); //单位分
                long second = (millisUntilFinished - day * (1000 * 24 * 60 * 60) - hour * (1000 * 60 * 60) - minute * (1000 * 60)) / 1000;//单位秒
                Log.i("Log", "onTick: " + hour + "小时" + minute + "分钟" + second + "秒");
            }

            @Override//倒计时结束
            public void onFinish() {
                Log.i("Log", "onFinish: ");
            }
        }.start();
    }
上一篇 下一篇

猜你喜欢

热点阅读