ajncespedes
ajncespedes

Reputation: 33

Why CountDownTimer doesn't cancel?

I need to start a timer with initial 60 seconds but when i push the button, set 5 seconds or simply cancel but it doesn't work:

 temp = new CountDownTimer(60000, 1000) {
        Button acabar= (Button) findViewById(R.id.button_abandonar);
         public void onTick(long millisUntilFinished) {

             int seg = (int) (millisUntilFinished /1000);
             if(seg<10){
                 veces.setText("0" + seg);
             }else{
                 veces.setText("" + seg);
             }
         }

         public void onFinish() {
              //something

         }

      };

    if(v.getId()==R.id.view_pulsar){
        //Here I start temp and I do more things, but I don't show this code because is not relevant to this problem
    }else if(v.getId()==R.id.button_abandonar){
        temp.cancel();
        //temp = null;
        //here set 5 seconds, not 60 second I think cancel and restart it

}

Upvotes: 0

Views: 1153

Answers (2)

ajncespedes
ajncespedes

Reputation: 33

I just fix it, I initialized the timer on method onclick and it had various timer at the same time!

What cluelessness!

Upvotes: 1

zaPlayer
zaPlayer

Reputation: 862

can't see your temp.start();perhaps you should call it

edit reff http://developer.android.com/reference/android/os/CountDownTimer.html

 new CountDownTimer(30000, 1000) {

     public void onTick(long millisUntilFinished) {
         mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
     }

     public void onFinish() {
         mTextField.setText("done!");
     }
  }.start();

Upvotes: 0

Related Questions