I want to know. What can be happen, when I use AsyncTask in IntentService.
-
nothing will happens. IntentService runs already in Background. What do you need an Asynctask for? – Blackbelt Sep 29 '14 at 12:33
-
[Asynctask vs IntentService](https://groups.google.com/forum/#!topic/android-developers/p1AHcZqFeMI), [Its a choice of "or"](http://stackoverflow.com/questions/6875658/asynctask-executed-from-service-intentservice-and-activity-is-there-a-differ), [Use one, not both](http://stackoverflow.com/questions/15167152/should-i-use-asynctask-or-intentservice-for-my-application) – Pararth Sep 29 '14 at 12:36
-
@blackbelt it was only question. – userAsk Sep 29 '14 at 12:48
-
also my was just a question :) – Blackbelt Sep 29 '14 at 12:50
3 Answers
Once onHandleIntent()
returns, the service will be destroyed if there is no more outstanding work to be done. If the AsyncTask
is still running, you will have leaked that thread. There is no guarantee that your process will remain running long enough for the AsyncTask
to complete its work.
Since IntentService
already gives you a background thread -- the one that onHandleIntent()
runs on -- there is no need for another background thread. Just put your doInBackground()
logic in onHandleIntent()
of the IntentService
.
Also, in general, using an AsyncTask
from any service is unnecessary, as the service has no need to do anything on the main application thread.

- 986,068
- 189
- 2,389
- 2,491
-
is there anything similar **onPostExecute()** in services?It came to my rescue a number of tasks while using AsyncTasks...anyways +1 – nobalG Sep 29 '14 at 12:54
-
1@Butterflow: ""is there anything similar onPostExecute() in services?" -- no, because you rarely want to do anything in a service explicitly on the main application thread. – CommonsWare Sep 29 '14 at 12:57
-
@CommonsWare Would the thread be leaked too if the AsyncTask were called with application context instead of the service context? – jmart Jul 25 '16 at 18:55
-
@jmart: Yes. Here, "leaked" means that there is no running Android activity or service managing the thread and letting Android know that the app is still trying to do work. – CommonsWare Jul 26 '16 at 00:07
You can use aynchronous task inside intent service and it will run perfectly.Since , intentService is to run the background task without Ui interaction but it is better for one work not to use asychronous task inside intentService.It will be helpful when You are using more than one long operations in the intentService otherwise that will struck the device

- 3,845
- 3
- 14
- 14
I think it is a completely bad idea to execute AsyncTask inside IntentService. I have an experience of AsyncTask inside JobIntentService which is different from IntentService but it causes crash. Just put codes inside doInBackground() in onHandleIntent().

- 409
- 3
- 6