I want to fire an event (sending a sms) every 2 minute . how can i do this . I am using AsyncTask Class to handle background task and want to use its object to execute 5 times with an interval of 2 minutes .I have seen the previous answers and try using Thread.sleep() ,Timer ,and wait but nothing worked for me . Please help guys.
Asked
Active
Viewed 55 times
1
-
Do you want them to fire at equal intervals (every 24 seconds)? When the previous task is done? Something else? – Gabe Sechan Jul 27 '14 at 05:49
-
yes I want to fire at equal intervals .Its ok if delayed but I don't want it to execute earlier than specified interval. – meShakti Jul 27 '14 at 05:53
1 Answers
0
private static final ScheduledExecutorService worker =
Executors.newSingleThreadScheduledExecutor();
void someMethod() {
⋮
Runnable task = new Runnable() {
public void run() {
/* Do something… */
}
};
worker.schedule(task, 5, TimeUnit.SECONDS);
⋮
}
or if you want to use timer
you can refer to this post on stackoverflow

Community
- 1
- 1

Umer Kiani
- 3,783
- 5
- 36
- 63
-
1You should indent your code nicer so it's easier to read... you'll get more upvotes if you do. – Alex Lockwood Jul 27 '14 at 06:42
-
-
Thank you @UmerKiani your code let me implement all the functionality I want to implement. – meShakti Jul 27 '14 at 14:06
-