nexus490
nexus490

Reputation: 817

Countdown clock methods

I have a CountDownTimer in my app setup like this:

new CountDownTimer(11000, 1000) {

     public void onTick(long millisUntilFinished) {
        clock.setText("Seconds Remaining: " + millisUntilFinished / 1000);
        secsrem = (int) millisUntilFinished / 1000;
     }

     public void onFinish() {
         //...}
  }.start();

I need to access the onFinish method in another method in my app so that when a button is pressed it will call the timers onFinish method. How would i do this?

Upvotes: 0

Views: 1058

Answers (1)

Caner
Caner

Reputation: 59168

Just store your timer in a variable:

public class YourClass {

public CountDownTimer timer = null;

...
timer = new CountDownTimer(11000, 1000) {

         public void onTick(long millisUntilFinished) {
            clock.setText("Seconds Remaining: " + millisUntilFinished / 1000);
            secsrem = (int) millisUntilFinished / 1000;
         }

         public void onFinish() {
             //...}
      }

timer.start();
...

Then you can do:

timer.onFinish();

Btw, I think you should not call onFinish yourself, it will be called by system when the time is up. If you want to cancel the timer instead use:

timer.cancel();

Upvotes: 1

Related Questions