I'd like to make a countdown timer do a continuous loop in android - need opinions on how to do it
android, continuous, countdown, loops, timer
Solution
One very basic way to loop a CountDownTimer is to call `start()` in `onFinished()`.
public void onFinish() {
...
start(); // Start this timer over
}
(Make sure you cancel your CountDownTimer in `onPause()` when you do this otherwise the timer might leak and keep firing in the background... Oops.)
However CountDownTimers has fundamental flaws (in my opinion): it often skips the last call to `onTick()` and it gains a few milliseconds each time `onTick()` is called... :( I re-wrote CountDownTimer in a previous question to be more accurate and call every tick.
Problem
Is there a way to continuously loop through a countdown timer? I have a basic timer of going though 60 seconds, then updating a text field and it's working, but I want to add the functionality: when it's counted down, to automatically restart again until the user cancels it? Maybe run it through a thread? Not sure how to handle it. Here is what I have, and again, this code works, but I can only stop and start the countdown timer, not do a continuous loop: ``` cdt = new CountDownTimer(60000,1000) { public void onTick(long millisUntilFinished) { tvTimer.setText("remaining : " + millisUntilFinished/1000 + " secs"); } public void onFinish() { tvTimer.setText(""); bell.start(); } }; /***************On Click/Touch Listeners*******************/ btnNext.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { tvTimer.setText(""); btnStart.setText("Start Timer"); SetImageView2(myDbHelper); cdt.cancel(); } }); btnStart.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if (!TimerTicking){ cdt.start(); btnStart.setText("Stop Timer"); } else { tvTimer.setText(""); cdt.cancel(); btnStart.setText("Start Timer"); } } }); ```