Ankuj
Ankuj

Reputation: 723

Asynchronous Repeating Scheduled Task

How does one create an AsyncTask which keeps running itself after a fixed interval of time.

For eg. get data from server every 5 minutes and give notification to caller thread that it has received the data. I searched on the forum but could not find much. What I have gathered so far is that

1) A UI thread will call AsyncTask

2) onPrExecute for UI thread access before executing

3) OnPostExecute for UI thread access after executing

I dont need to show any progress update to the user. Also, the task will be destroyed when the app closes. Any tutorial for this will he helpful

Upvotes: 1

Views: 1194

Answers (4)

Rohit Sharma
Rohit Sharma

Reputation: 13815

As other suggested in the comments. So let me elaborate it more.

DON'T USE AsyncTask. INSTEAD GO FOR IntentService ONLY.

  1. Make a class extends IntentService
  2. Use Alarm Manager to trigger a intent to your own service with specific action item
  3. Define a Interface to be used to notify client of this service.
  4. Maintain a list of these interface implemenation object provided by its client.
  5. in onHandleIntent(Intent intent) identify the call by checking action.
  6. Initiate the data fetch request directly on intentService as the use worker thread to work and in the end call update delegate of the interface object list you maintained.

  7. Make methods to Let Activity register and unregister from listening these updates.

  8. Which is actually adding the interface implementation provided by them in the list you maintained and remove from it when unregister gets called.
  9. make sure activity register itself in onResume and unregister itself in the onPause.
  10. Either use Repeating alarm or initiate one again in the end of single operation run. I hope it helps :)

Upvotes: 2

MLQ
MLQ

Reputation: 13511

I wrote an app that fires AsyncTasks on a regular interval, except that they persist even when the app is closed. Anyway, here's what I had to do:

  1. Create a PendingIntent (via getBroadcast()) that contains an Intent that contains an action.
  2. Supply the PendingIntent to the system's AlarmManager and set the intervals.
  3. Declare a BroadcastReceiver in the manifest to catch the action string supplied to the Intent in no. 1.
  4. In the onReceive() method of your BroadcastReceiver, fire the AsyncTask.

Upvotes: 0

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

Try this..

- Better use Service to make this work again and again.

- Now you can use bound or unbound Service. If you want the Service to be bounded to the Activity then use Bound Service else use UnBound Service.

- If would be even better to use IntentService, as here you don't need the task keep running, but runs after certain amount of time.

See this link:

http://mobile.tutsplus.com/tutorials/android/android-fundamentals-intentservice-basics/

Upvotes: 0

Phil
Phil

Reputation: 36289

You can schedule the AsyncTask to repeat at a fixed rate using Timer.scheduleAtFixedRate.

Upvotes: 0

Related Questions