Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop the Timer in android? [closed]

Tags:

android

timer

I'm developing an application which sends a message to a specific number in a specific period of time. The problem is that it continues sending that message after that period of time. How would I stop the timer after that specific time in order to stop sending that message?

like image 650
Krishna Avatar asked Feb 14 '11 15:02

Krishna


People also ask

How do you set a timer on android?

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(). start() - It is used to call countdown timer.

How do I know if a countdown timer is running?

After start -> make it true; Inside OnTick -> make it true (not actually required, but a double check) Inside OnFinish -> make it false; use the isTimerRunning property to track the status. Show activity on this post. Check if the CountDownTimer is Running and also if the app is running in the background.

What is delay and period in Timer Android?

delay - delay in milliseconds before task is to be executed. period - time in milliseconds between successive task executions. (Your IDE should also show it to you automatically) So delay is the time from now till the first execution, and after that it executes every period milliseconds again.


2 Answers

     CountDownTimer waitTimer;      waitTimer = new CountDownTimer(60000, 300) {         public void onTick(long millisUntilFinished) {           //called every 300 milliseconds, which could be used to           //send messages or some other action        }         public void onFinish() {           //After 60000 milliseconds (60 sec) finish current            //if you would like to execute something when time finishes                  }      }.start(); 

to stop the timer early:

     if(waitTimer != null) {          waitTimer.cancel();          waitTimer = null;      } 
like image 125
ice911 Avatar answered Sep 24 '22 07:09

ice911


and.. we must call "waitTimer.purge()" for the GC. If you don't use Timer anymore, "purge()" !! "purge()" removes all canceled tasks from the task queue.

if(waitTimer != null) {    waitTimer.cancel();    waitTimer.purge();    waitTimer = null; } 
like image 29
cmcromance Avatar answered Sep 24 '22 07:09

cmcromance