Proper way to stop IntentService

若如初见. 提交于 2019-11-27 09:03:01

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.

An IntentService stops itself when it has no more Intents (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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!