What is the best practice to schedule an AsyncTask
to have it run every minute (note that after the AsyncTask has finished I should be able to update the UI).
I'm not intending on using a Service
because these tasks should only run when app is active.
EDIT: What the AsyncTask is just downloading JSON data from a webserver (which I need to update UI). The JSON data is pretty small a couple of kilobytes.
You have to use a
Timer
:timer.schedule(asyncTask, 0, 50000); //execute in every 50000 ms
You can update your UI in the
onPostExecute()
overrided method of theAsyncTask
class.http://developer.android.com/reference/java/util/Timer.html http://developer.android.com/reference/android/os/AsyncTask.html
I'd use a
Timer
object.There's a full example: