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?
-
3I'd help if you'd show some of your code that employs the timer. – Peter Knego Feb 14 '11 at 15:17
-
2Don't understand how this is an off-topic question! It has got 15 up votes, 8 stars also accepted answer has 45 ups!! – A. K. M. Tariqul Islam Mar 02 '17 at 04:37
7 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;
}

- 1,180
- 7
- 9
-
-
2i have tried to stop timer early with this way but it is not stopped.What may be issue? – βhargavḯ May 24 '12 at 08:39
-
1
-
1
-
-
1
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;
}

- 9,807
- 6
- 53
- 76

- 2,289
- 1
- 25
- 26
-
-
1@IgorGanapolsky I think it depends on the case. The answer is .. maybe.. when we don't need the timer anymore. If you call codes above in onPause(), you should create the timer onResume(). If in onDestory(), create the Timer in onCreate(). Also, let us suppose that we have two button for Start and Stop timer. When users click Start button, we can create the timer. Then, when users click Stop button, we should destroy the timer. :) – cmcromance Aug 02 '13 at 00:36
-
4There is no need to call `Timer.purge()` because `waitTimer = null` marks the entire structure for GC already. The purpose of `purge()` is to discard tasks that have been canceled with `TimerTask.cancel()`. – Oderik Jul 15 '14 at 11:23
-
@Oderik Maybe you are right. But I like to implement something safely. Because I cannot check all the things behind my codes. I am not a genius :) I saw the comment about perge() ; "Removes all canceled tasks from the task queue. If there are no other references on the tasks, **then after this call they are free to be garbage collected.**" – cmcromance Jul 13 '15 at 04:23
-
-
@RuchirBaronia When I make the object null like that, GC can clean it as soon as possible. – cmcromance Nov 26 '15 at 12:18
In java.util.timer one can use .cancel()
to stop the timer and clear all pending tasks.

- 79,991
- 11
- 123
- 154
-
7but actually this is not true, I called .cancel() and the timer didn't stop! – bebosh Feb 02 '15 at 15:19
-
-
timer.cancel(), this is not true , I called cancel and timer didn't stop – Keshav Gera Aug 08 '19 at 13:07
We can schedule the timer to do the work.After the end of the time we set the message won't send.
This is the code.
Timer timer=new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
//here you can write the code for send the message
}
}, 10, 60000);
In here the method we are calling is,
public void scheduleAtFixedRate (TimerTask task, long delay, long period)
In here,
task : the task to schedule
delay: amount of time in milliseconds before first execution.
period: amount of time in milliseconds between subsequent executions.
For more information you can refer: Android Developer
You can stop the timer by calling,
timer.cancel();

- 377
- 4
- 5
I had a similar problem: every time I push a particular button, I create a new Timer.
my_timer = new Timer("MY_TIMER");
my_timer.schedule(new TimerTask() {
...
}
Exiting from that activity I deleted the timer:
if(my_timer!=null){
my_timer.cancel();
my_timer = null;
}
But it was not enough because the cancel()
method only canceled the latest Timer. The older ones were ignored an didn't stop running. The purge()
method was not useful for me.
I solved the problem just checking the Timer
instantiation:
if(my_timer == null){
my_timer = new Timer("MY_TIMER");
my_timer.schedule(new TimerTask() {
...
}
}

- 7,432
- 9
- 58
- 91

- 51
- 3
-
2Would make life so much easier if all the C programmers could start to use cammelCase while programming in Java – Lukas Hanacek Dec 20 '16 at 08:31
I had a similar problem and it was caused by the placement of the Timer initialisation.
It was placed in a method that was invoked oftener.
Try this:
Timer waitTimer;
void exampleMethod() {
if (waitTimer == null ) {
//initialize your Timer here
...
}
The "cancel()" method only canceled the latest Timer. The older ones were ignored an didn't stop running.

- 41
- 1
- 5
It says timer() is not available on android? You might find this article useful.
http://developer.android.com/resources/articles/timed-ui-updates.html
I was wrong. Timer() is available. It seems you either implement it the way it is one shot operation:
schedule(TimerTask task, Date when) // Schedule a task for single execution.
Or you cancel it after the first execution:
cancel() // Cancels the Timer and all scheduled tasks.

- 6,434
- 7
- 35
- 44
-
-
1It might have been removed. Read it here: http://developers.androidcn.com/resources/articles/timed-ui-updates.html – dongshengcn May 06 '15 at 15:59