2016-10-23 4 views
2

Comment arrêter ce minuteur, une idée?Comment arrêter CountDownTimer dans android

Je veux réinitialiser le temporisateur dans chaque requête, mais cela continue. Chaque nouvelle requête ajoute un nouveau timer. Comment résoudre ceci?

new CountDownTimer(zaman, 1000) {      //geriye sayma 

      public void onTick(long millisUntilFinished) { 

       NumberFormat f = new DecimalFormat("00"); 
       long hour = (millisUntilFinished/3600000) % 24; 
       long min = (millisUntilFinished/60000) % 60; 
       long sec = (millisUntilFinished/1000) % 60; 

       cMeter.setText(f.format(hour) + ":" + f.format(min) + ":" + f.format(sec)); 
      } 

      public void onFinish() { 
       cMeter.setText("00:00:00"); 
      } 
     }.start(); 

Répondre

14

vous devez définir comme une variable

CountDownTimer yourCountDownTimer = new CountDownTimer(zaman, 1000) {      //geriye sayma 

     public void onTick(long millisUntilFinished) { 

      NumberFormat f = new DecimalFormat("00"); 
      long hour = (millisUntilFinished/3600000) % 24; 
      long min = (millisUntilFinished/60000) % 60; 
      long sec = (millisUntilFinished/1000) % 60; 

      cMeter.setText(f.format(hour) + ":" + f.format(min) + ":" + f.format(sec)); 
     } 

     public void onFinish() { 
      cMeter.setText("00:00:00"); 
     } 
    }.start(); 

yourCountDownTimer.cancel(); 

En savoir plus: https://developer.android.com/reference/android/os/CountDownTimer.html

+0

Merci beaucoup yourCountDownTimer .Annuler(); fonctionne – Taha

+0

de rien. il aide à accepter ma réponse @Taha –

+0

yourCountDownTimer.stop()? Devrait-il être .cancel()? –

1

Vous pouvez appeler this.cancel() ou simplement cancel() dans l'une de ses méthodes de rappel