If I execute an asynctask in one of my services, can there ever be a circumstance where android stops the asynctask for some reason, say for example when it is taking too long?
Asked
Active
Viewed 47 times
0
-
See https://stackoverflow.com/questions/7882739/android-setting-a-timeout-for-an-asynctask – Ian D Oct 11 '18 at 15:36
-
@IanDeLaney thanks for the reply, but this doesn't help me. I don't want to cancel the asynctask; can it be stopped by the android OS itself under any circumstances? – Jay Gumballi Oct 11 '18 at 15:42
-
Make the service as a Singleton, accessible by your activity. That way you can access to your asynctask, on service, and do whatever you want – PedroHawk Oct 11 '18 at 15:53
-
[This post](https://stackoverflow.com/questions/2531336/asynctask-wont-stop-even-when-the-activity-has-destroyed) seems to indicate an AsyncTask can run for a long time. Nevertheless I think they can be killed in low memory conditions like every other part of a process so they're not the ticket to the immortal background thread – Bö macht Blau Oct 11 '18 at 15:53
-
Set you own conditions like setconnectiontimeout for url connection then cancel your asyntask on that condition. – Mohamed Mohaideen AH Oct 11 '18 at 15:58
1 Answers
0
If you don't get any response from your task you can call:
asyncTask.cancel(true);
Then in your doInBackground() method you need to check if it was cancelled before you try to use the callback data:
if (isCancelled())
[EDIT 1] If you don't want to cancel it, and you are wondering if the OS can stop your task, the answer is Yes. The Android OS can stop your app from using any resource if it decides that something else needs it more than your app does, specially if your app is not in the foreground.

PedroMazarini
- 116
- 7