I want to run a specific method doThis()
(which is not a CPU intensive action, it's just a POST to a web server) every 10 minutes and when that method is finished executing, to notify the user (via Toast
or Notification
or something of the sort). But I don't want to block up the UI thread because I expect the user to still be interacting with the UI. What is the most efficient way to do this? Through a Handler
? Or a Service
or some sort (i.e. IntentService
)?

- 53,877
- 76
- 193
- 251
4 Answers
The most clean and efficient way IMO is to use ScheduledThreadPoolExecutor, check out my answer here for the code example, hope this help.
-
After I call "shutdown()". If I choose to start the same ScheduledThreadPoolExecutor back up, will I be allowed to? Or will I have to create instance? – Mohit Deshpande Dec 21 '11 at 13:52
-
And will the Executor be running in the background if the user changes applications? – Mohit Deshpande Dec 21 '11 at 14:20
-
@MohitDeshpande, good question, after calling shutdown(), if you want use it again, you need initialize a new one. otherwise get RejectedExecutionException when trying to submit new task. For your second question, I suppose yes but not 100% sure, better to try it out youself. – yorkw Dec 21 '11 at 19:52
How about an AsyncTask?
The doInBackground
method will help you run your specific method in the background for the specific interval without interacting with the UI thread.
The onPostExecute
will help you notify the user using the Toast

- 4,200
- 15
- 79
- 129
I do this via an AsyncTask
. The doInBackground()
method looks something like this (just a pseudocode):
doInBackground(String... params){
while (running){
doThis();
Thread.sleep(10 * 1000);
}
}
I would like to add that my app performs these updates (GETs and POSTs) more frequently (every 5-10 seconds) and displays the new values to the user - which is why I use an Activity. You could fire off the AsyncTask from a Service instead.
The other option you have is to use an AlarmManager
- although I haven't been down that path.

- 10,572
- 10
- 61
- 110
-
Will the AsyncTask still be running in the background if the user switches applications? – Mohit Deshpande Dec 21 '11 at 14:27
-
Yes it will .. unless you take steps to prevent this from happening. In my case, I start the `AsyncTask` from an Activity. In `onPause()`, I "pause" the task (using some custom logic). If you start the task from a `Service`, you could choose to allow it to continue in the background. – curioustechizen Dec 22 '11 at 04:47
I'll recommend Timer class as you stated you have to perform timed task. In case you need to perform this action while your activity is running you can use Timer class for the same in Activity itself else you can put it up in Services to run in the background..
Refer to this link. : http://developer.android.com/resources/articles/timed-ui-updates.html

- 1,030
- 9
- 19
-
I'm against the Timer suggestion. I've had nothing but problems with these guys, especially in regards to exception handling. Timer pretty much just swallows them leaving you wondering weather its worked or not. – Bob Kuhar Dec 20 '11 at 04:29
-
I am sorry to hear about what you experienced but till date i am having perfect functioning of it. – Abhinava Dec 20 '11 at 08:55