Asynchronous re-scheduled task
How to create AsyncTask
one that continues to work after a certain period of time.
For example, receive data from the server every 5 minutes and notify the caller thread that it has received the data. I searched the forum but couldn't find many. So far, I realized that
1) The UI thread will call AsyncTask
2) onPrExecute
to access the UI thread before executing
3) OnPostExecute
to access the UI thread after execution
I don't need to show the update to the user. In addition, the task will be destroyed when the application is closed. Any tutorial for this will help him
source to share
As in the comments. So let me elaborate on this.
DO NOT USE AsyncTask. INSTEAD GO ONLY for IntentService.
- Make class extends IntentService
- Use an alert manager to trigger your own service intent with a specific action.
- Define the interface that will be used to notify the client of this service.
- Maintaining a list of this object's implementation of the interface provided by its client.
- in onHandleIntent (intent intent) identifies the call by validating the action.
-
Initiate a fetch request directly for the intent as a workflow to work with, and in the call completion updates from the list of interface objects that you supported.
-
Make methods to allow the activity to register and unregister while listening for these updates.
- Which actually adds the implementation of the interface they provided in the list you maintained and removes from it when you call unregister.
- make sure the activity is registered in onResume and unregister in onPause.
- Use re-alarm or trigger it again at the end of a single trigger. Hope this helps :)
source to share
Try it.
- Better to use Service
to do the job over and over.
- You can now use tethered or unbound Service
. If you want the service to be limited to Activity, use Bound Service else to use UnBound Service.
- If it would be better to use IntentService
, since here you don't need the task to keep running, but starts after a certain time.
See this link:
http://mobile.tutsplus.com/tutorials/android/android-fundamentals-intentservice-basics/
source to share
I wrote an app that launches AsyncTask
at a regular interval, except they persist even when the app is closed. Anyway, here's what I had to do:
- Create
PendingIntent
(viagetBroadcast()
) which containsIntent
which contains action. - Put
PendingIntent
in the systemAlarmManager
and set the intervals. - Declare
BroadcastReceiver
in manifest to catch the action string specifiedIntent
in no. 1. - In
onReceive()
your method,BroadcastReceiver
runAsyncTask
.
source to share