Hello. I use the following code to work with the timer, how can I stop it before the scheduled time?

new CountDownTimer(1000, 1000) { public void onTick(long millisUntilFinished) { final int msek = (int) millisUntilFinished/10; // получаем мсек t1.setText(String.valueOf(Time)+"."+String.valueOf(msek)); } public void onFinish() { } }.start(); 
  • 3
    Well, judging by the fact that there are only 4 methods in it and one of them is cancel, then probably it is ... - JEcho
  • It is obvious! Thank. - Kota1921
  • nothing is clear how to use it? - user277566

1 answer 1

First save to a separate variable:

  CountDownTimer myTimer= new CountDownTimer(... 

And then call the cancel method:

 myTimer.cancel();