0

I watched TheNewBostons Video on countdown timers in Java, but he just showed how to display them.

I want to know how to get them to work and actually count downwards. So how would I get a timer that starts of with 5 and counts down to 0 and then do something at 0 and after it did something start the counter again and keep repeating it?

Lorenzo Dematté
  • 7,638
  • 3
  • 37
  • 77

2 Answers2

0

Here is the exmaple

create a object of the class given below like this

final CounterClass timer = new CounterClass(180000,1000); // 1800000 and 1000 are in milli seconds 

here 180000 is equal to 3 minutes

180000/1000 = 180 seconds and 180/60 = 3 minutes

and 1000 in CounterClass(180000,1000); means interval between the next tick

and for 5 seconds use 5000 instead of 1800000

Start the timer

  timer.start();

end the timer

   timer.cancel();  

here is the counter class

     public class CounterClass extends CountDownTimer {  
          public CounterClass(long millisInFuture, long countDownInterval) {  
               super(millisInFuture, countDownInterval);  
          }  
          @Override  // when timer is finished
         public void onFinish() {  
           System.out.println("Completed.");  
         }  
          @Override  // on every tick of the timer
          public void onTick(long millisUntilFinished) {  
                long millis = millisUntilFinished;  
                 String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),  
                     TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),  
                     TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));  
                 System.out.println(hms);  
                 
          }  
     }  
}
Community
  • 1
  • 1
Jamil
  • 5,457
  • 4
  • 26
  • 29
  • Hey thanks this helped a lot!. How do i check when the timer is finished or reaches 0 in another class. – XetronProgrammer Sep 08 '14 at 16:34
  • public void onFinish() { System.out.println("Completed."); } – Jamil Sep 08 '14 at 17:12
  • what about checking if the timer has reached 0 with an if statement and not calling the onFinish() method. How do i do that? – XetronProgrammer Sep 08 '14 at 19:28
  • onFinish function means the timer has stopped.. so this function will execute after timer has stopped.. you can implement here what you want to do after the timer has finished... so no need of if else becuase onFinish() functions is there for you :)...BUT.. if you want it then in onTick Method you can implement it by this... if (Hours == 0 && Minutes == 0 and Seconds == 0){} else {}.. i hope it helps you. – Jamil Dec 16 '14 at 12:31
0

Every CountDownTimer has or should have a onFinish() Method, Here you can start somthing new when the timer is over.

for example:

public void onFinish(){

   textView.setText("Time is over");

   this.start();//this starts the timer again(and again and again...(in the onFinish))


}

Use the timer from the other answer. This should answer part 2 of your question.

Btw. Google has super good examples for Android CountDownTimers. In case you want search for it.