tags:

views:

65

answers:

3

I am getting data from the server using AsyncTask. I need to update the data periodically.

Whats the best way to do it?

A: 

You can do it either on your onProgressUpdate() or on onPostExecute() based on your requirement.

onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...). The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field.

onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.

http://developer.android.com/intl/de/reference/android/os/AsyncTask.html

Rahul
A: 

Set an alarm with AlarmManager and call your AsyncTask in your AlarmReceiver class.

Macarse
Hi Macarse,Thanx for the answer. But can u tell me how can I call my AsyncTask class which is an inner class of my activity?
Kunal P.Bharati
it should be defined as `public static class` and call it with: `(new YourActivity.YourAsyncTask()).execute();`
Macarse
+2  A: 

Hi,

You could use Timer class to schedule periodic task using TimerTask instead of AsyncTask

See :

http://developer.android.com/reference/java/util/Timer.html

http://developer.android.com/reference/java/util/TimerTask.html

And to update your UI you should follow this good tutorial :

http://developer.android.com/resources/articles/timed-ui-updates.html

Mathieu
Thanx :) Worked for me.
Kunal P.Bharati