3

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");

        }
       }
    });
Kristy Welsh
  • 7,828
  • 12
  • 64
  • 106

2 Answers2

6

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.

Community
  • 1
  • 1
Sam
  • 86,580
  • 20
  • 181
  • 179
  • So a call to cancel() will then terminate the whole countdown? – Kristy Welsh Nov 17 '12 at 02:44
  • Yes, I did a quick test and saw that simply calling `start()` somehow started multiple timers... So use `cancel()` to prevent this. – Sam Nov 17 '12 at 02:49
  • I don't want to spawn multiple timers, so not sure that will be the best solution. I'm new to Android and it seems like doing a threaded solution might be best, but CountDownTimer is so convenient. – Kristy Welsh Nov 17 '12 at 02:52
  • Is it possible to have a new countdown timer with a different timer after the first one exits? So, I am looking for a way to have one timer start from 10 count down and once finished, another timer starts with lets say 15 and counts down and so on. This can happen for multiple times. Is there a way to achieve this? – Durga Swaroop Dec 28 '16 at 15:31
0

The first value to the CountDownTimer() constructor, the total time to run, is a long and can hold a value approaching 300 million years. That ought to be long enough for most mobile applications. :-)

So just call it with cdt = new CountDownTimer(Long.MAX_VALUE, 1000) for a one second loop that will run continously.

scottt
  • 8,301
  • 1
  • 31
  • 41