1

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.

meShakti
  • 273
  • 1
  • 11
  • 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 Answers1

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

Android timer? How-to?

Community
  • 1
  • 1
Umer Kiani
  • 3,783
  • 5
  • 36
  • 63