Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop CountDownTimer in android

How to stop this timer , any idea ?

I want to reset timer in every query but it continues. Every new query it adds new timer. How to solve this?

 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(); 
like image 578
Taha Avatar asked Oct 23 '16 14:10

Taha


People also ask

How do you keep a CountDownTimer running even if the app is closed?

To keep the timer running while the app is closed, you need to use a service. Listen to the broadcast of the service in the activity. See this SO answer to learn whether registering the receiver in onCreate, onStart, or onResume is right for you.

How do I set a countdown on my Android?

we can set count down time after completion of time it will stop and get 0 values. onTick(long millisUntilFinished ) - In this method we have to pass countdown mill seconds after done countdown it will stop Ticking. onFinish() - After finish ticking, if you want to call any methods or callbacks we can do in onFinish().

How do I see remaining time on Android?

getTime() - CurrentTime. getTime()) / 1000;//in seconds Log. d(TAG, "startFajrAlert: remainingTime: " + remainingTime); Intent intent = new Intent(getContext(), FajrReceiver.


1 Answers

You can assign it to a variable and then call cancel() on the variable

CountDownTimer yourCountDownTimer = new CountDownTimer(zaman, 1000) {                         public void onTick(long millisUntilFinished) {}      public void onFinish() {}      }.start();  yourCountDownTimer.cancel(); 

or you can call cancel() inside of your counter scope

new CountDownTimer(zaman, 1000) {                         public void onTick(long millisUntilFinished) {         cancel();     }      public void onFinish() {}      }.start(); 

Read more: https://developer.android.com/reference/android/os/CountDownTimer.html

like image 89
Amir_P Avatar answered Sep 20 '22 00:09

Amir_P