简体   繁体   中英

CountDownTimer in Android using Java

I am trying to implement a timer in android. I am using the countdowntimer. Here, is the timer code:

 new CountDownTimer(totalTime * 1000, 1000) {
        @Override
        public void onTick(long l) {
            int newSec = (int) (sec - (sec - (l / 1000)));
            if(l % 60 == 0) {
                min--;
            }
            if(newSec < 10) {
                tv_timer.setText(min + ":" + z + newSec);
            } else {
                tv_timer.setText(min + ":" + newSec);
            }
        }

        @Override
        public void onFinish() {
            mediaPlayer.start();
        }
    }.start();

The timer keeps updating the amount of time left on the user's screen.

The problem is the timer keeps ending at 00:01 (1 second), it never displays 00:00 and the alarm tone rings 2 seconds after the timer ends.

How can i get 00:00 to be displayed and how to get the alarm tone to ring immediately after the timer ends?

This is the seekbar code. The seekbar is used by the user to set the timer:

seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
            z = 0;
            min = i / 60;
            sec = i - min * 60;
            if(sec < 10) {
                tv_timer.setText(min + ":" + z + sec);
            } else {
                tv_timer.setText(min + ":" + sec);

            }
            totalTime = i;
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

As shown in the CountDownTimer documentation

onTick(long millisUntilFinished)

millisUntilFinished long: The amount of time until finished.

Which means that 00:00 won't ever be shown in your code because the onTick won't be called when there is no time left to be finished.

Simply you have to show the 00:00 inside onFinish

@Override
        public void onFinish() {
            tv_timer.setText("00:00");
            mediaPlayer.start();
        }

About the alarm tone, seems that it rings immediately after the count down is finished. You have to check that the tone itself doesn't have a silent one or two seconds in the beginning.

Following up on @ShadySherif 's (correct) answer, adding some basic logging will help in understanding the behavior and highlight a suspect statement in your code.

Snippet:

// Countdown 10 seconds providing an update every second until finished.
new CountDownTimer(10 * 1000, 1000) {
    @Override
    public void onTick(long millisUntilFinished) {
       Log.d("sotest",Long.toString(millisUntilFinished));
    }

    @Override
    public void onFinish() {
        Log.d("sotest","Finish");
    }
}.start();

Log (note the millisUntilFinished values will vary each run):

2020-07-10 09:32:46.628 9087-9087/calculations D/sotest: 9986
2020-07-10 09:32:47.629 9087-9087/calculations D/sotest: 8985
2020-07-10 09:32:48.630 9087-9087/calculations D/sotest: 7983
2020-07-10 09:32:49.633 9087-9087/calculations D/sotest: 6981
2020-07-10 09:32:50.636 9087-9087/calculations D/sotest: 5978
2020-07-10 09:32:51.640 9087-9087/calculations D/sotest: 4974
2020-07-10 09:32:52.643 9087-9087/calculations D/sotest: 3970
2020-07-10 09:32:53.647 9087-9087/calculations D/sotest: 2967
2020-07-10 09:32:54.650 9087-9087/calculations D/sotest: 1964
2020-07-10 09:32:55.653 9087-9087/calculations D/sotest: 961
2020-07-10 09:32:56.616 9087-9087/calculations D/sotest: Finish

One suspect statement is this:

// Incorrect, this performs:
//   "every time the millisUntilFinished value is a multiple of 60,
//   decrement what most likely is a minute value."
if(l % 60 == 0) {
    min--;
}

So it looks like you are trying to maintain a minutes value by performing a modulo on a milliseconds value (which has error in it as well). This is not correct.

So the point is simple logging can help a great deal in understanding issues.

Also, from the log you can see an interesting behaviour which is the "drift" of the value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM