I'm using an IntentService to upload images to a server. My problem is that I don't know how/when to stop the service. When I call stopself() in onHandleIntent(Intent ..) all Intents which are waiting in the IntentService queue are removed. But I don't want to stop the service from an activity because I want to complete upload proccess even if my application is not running.
2 Answers
My problem is that I don't know how/when to stop the service.
IntentService
automatically stops itself when onHandleIntent()
ends, if no more commands had been sent to it while onHandleIntent()
was running. Hence, you do not manually stop an IntentService
yourself.
When I call stopself() in onHandleIntent(Intent ..) all Intents which are waiting in the IntentService queue are removed.
Which is why you do not do that.
But I don't want to stop the service from an activity because I want to complete upload proccess even if my application is not running.
Then you let the IntentService
stop itself.

- 986,068
- 189
- 2,389
- 2,491
-
Thank you, I wasn't aware of the fact that IntentService stops itself when it's done.:] – malinjir Apr 20 '12 at 17:39
-
3One more jagged corner in the Android SDK... sigh. – markshiz Mar 20 '15 at 13:54
-
I have this problem here the supposed to be 25 seconds to 1min task don't end so my intentService don't finish - https://stackoverflow.com/questions/39976001/force-stop-a-stuck-intentservice-programmatically – mboy Dec 08 '16 at 06:15
An IntentService
stops itself when it has no more Intent
s (jobs) to process. It runs stopSelf(startId)
for each Intent
(job). Have a look at the IntentService
source or the Extending the Service Class
from here http://developer.android.com/guide/topics/fundamentals/services.html

- 8,473
- 4
- 39
- 42