If the AsyncTask finishes executing, the thread on which the asynctask was run just waits (Object.wait()). The AsyncTask is based upon the ExecutorService and FutureTask classes from the java.util.concurrent packages.
Creating a new AsyncTask does not create a new thread. An AsyncTask is executed on one thread selected from a pool of threads (internally it uses ExecutorService from java.util.concurrent) If you want to have ONE thread that continuously runs in the background (e.g. wait 5 mins, see if there's data, if so notify the main app, start over again), use a Thread, not an AsyncTask. When you have a 'while(true)' or some similar loop in your background-task code, it may be a sign that you need a Thread instead. Then in that Thread you can have a reference to your activity (e.g. mActivity) and call runOnUiThread() on your activity. Here is a code- example snippet from that Thread: public void run() { while (!mStopped) { sleep(fiveMinutes); final Result result = getSomeDataFromSomehwere(); if (result.isNotEmpty()) { mActivity.runOnUiThread(new Runnable() { public void run() { mActivity.hereIsResult(result); } }); } } } (above is pseudo-code... it won't compile :-)) (and i assume that mActivity is still around and kicking when hereIsResult is called... if not, you have to use a static reference to your currently running activity instead) On Jan 22, 6:38 pm, Flapjack <millerhugg...@gmail.com> wrote: > Streets of Boston, please forgive my ignorance. If I use AsyncTask > within a service to retrieve data once, can I destroy the AsyncTask > after? Or would I have to destroy the entire Service? > > Additionally, if I want to poll, say, every 5 minutes, wouldn't having > AsyncTask in the service be a good idea, instead of tying up the > Service, which might be needed for other operations? > > Finally, what do you mean when you said to use a regular Thread that > calls 'post' on the owning Activity? > > On Jan 21, 10:37 pm, Streets Of Boston <flyingdutc...@gmail.com> > wrote: > > > > > A Service, like an Activity, has a main thread with a message-loop. > > You can create an AsyncTask in the onXXXXX() callbacks of Services, > > just like in an Activity. The onPostExecute can do your handling of > > the results, as you described. > > > However, AsyncTask is more geared towards the one-time executing of a > > background process. E.g. retrieve some data from the internet now, or > > create/process a bitmap. It is not really meant to stay in the > > background forever in a loop polling for some data to come in. When > > you have something like 'while (true)' or 'while (!mStopped)' or > > something like it, you should not use a AsyncTask. Use a regular > > Thread instead that calls 'post' on the owning Activity that will > > consume the results generated by the thread. > > > On Jan 21, 6:22 pm, Flapjack <millerhugg...@gmail.com> wrote: > > > > According to my research, which includes reputable sources (Mark > > > Murphy et al), the most preferred way of polling a remote source and > > > presenting said data to the user is by creating a service and using > > > AsyncTask within that service to do the polling. I have done that. > > > > But, when I read the docs (http://developer.android.com/reference/ > > > android/os/AsyncTask.html), there seem to be several "Threading Rules" > > > that conflict with this way of doing things: "The task instance must > > > be created on the UI thread." and "execute(Params...) must be invoked > > > on the UI thread." As stated, I have created the task instance on the > > > Service thread (not the UI thread). Am I missing something? > > > > Also, when the AsyncTask finished, I sent out a Broadcast on > > > onPostExecute, which is then picked up by the Activity, telling it to > > > retrieve the final value again from the service (since I couldn't > > > obviously update the UI from the service). I couldn't figure out any > > > other way to return the result of the AsyncTask. Is this the correct > > > practice? > > > > Java is not my native language so please bear with my ignorance. > > > Thanks!- Hide quoted text - > > - Show quoted text - -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en