How to Countdown to a day using Android CountDownTimer

后端 未结 3 870
野的像风
野的像风 2021-02-06 19:39

I\'m new here and new to Android Development, I Have a question regarding the Android CountDownTimer. How am I able to use Android\'s CountDownTimer, to count down to a specific

3条回答
  •  不知归路
    2021-02-06 20:16

    Helpful method !

    @Override
    public void start(final long miliSecond, final int interval) {
    
        new CountDownTimer(miliSecond, interval) {
            @Override
            public void onTick(long millisUntilFinished) {
                long day = TimeUnit.MILLISECONDS.toDays(millisUntilFinished);
                millisUntilFinished -= TimeUnit.DAYS.toMillis(day);
    
                long hour = TimeUnit.MILLISECONDS.toHours(millisUntilFinished);
                millisUntilFinished -= TimeUnit.HOURS.toMillis(hour);
    
                long minute = TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished);
                millisUntilFinished -= TimeUnit.MINUTES.toMillis(minute);
    
                long second = TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished);
    
                intDay.setText("" + day);
                intHour.setText("" + hour);
                intMinute.setText("" + minute);
                intSecond.setText("" + second);
            }
    
            @Override
            public void onFinish() {
                // What ever you want !
            }
    
        }.start();
    
    }
    

提交回复
热议问题