Proper way to stop IntentService

2019-01-09 09:00发布

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条回答
男人必须洒脱
2楼-- · 2019-01-09 09:40

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

查看更多
倾城 Initia
3楼-- · 2019-01-09 09:45

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.

查看更多
登录 后发表回答