0
String hms = String.format("%02d:%02d",
                    TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished)),
                    TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)));

I seriously don't get

1) How minutes - hours work TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished))

2) Why does it always ends with 00:01?

3) How does onTick() method of CountDowntimer gets called?

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
sofs1
  • 3,834
  • 11
  • 51
  • 89
  • use `DateUtils.formatElapsedTime` there is no need to reinent the wheel – pskink Nov 21 '16 at 09:50
  • @pskink DateUtils.formatElapsedTime is only for formatting time, right? or also to do the countDownTimer job? – sofs1 Nov 21 '16 at 10:25
  • `Formats an elapsed time in a format like "MM:SS" or "H:MM:SS" (using a form suited to the current locale), similar to that used on the call-in-progress screen.` – pskink Nov 21 '16 at 10:26

1 Answers1

1

1) How minutes - hours work TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished))

  • Don't know why you did this. You can just write

    String hms = String.format("%02d:%02d", TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished), TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)));

2) Why does it always ends with 00:01?

Yes you are right! In the countdown timer last call will not trigger the onTick() method it will call the below method.

 public void onFinish() {
    mTextField.setText("00:00");
}

So you can set "00:00" at the onFinish() method shown in the above snippet.

3) How does onTick() method of CountDowntimer gets called?

-onTick() Callback fired on regular interval provided to the CountDowntimer constructor.

android_griezmann
  • 3,757
  • 4
  • 16
  • 43