开发者

How to stop a service?

I have a service [IntentService] which runs a 'timer task' scheduled to ru开发者_C百科n after every 5 minutes. I start the service when my application is installed.

Now i want to stop my service [service runs a 'timer task'], but when i use stopService() method i am unable to stop my service. I tried to put a log in the onDestroy() method of my service but stopService() does not reaches there.

Also, since stopService() returns boolean i logged its output, it is returning false.

How should i stop it??


I have a service [IntentService] which runs a 'timer task' scheduled to run after every 5 minutes.

Please do not do this. Users hate developers who do this and will force-stop your application using task killers or the Settings app. Please use AlarmManager, so you can take up less RAM and be more friendly to the user.

I start the service when my application is installed.

This is not supported. In particular, as of Android 3.1, this is impossible, even by the undocumented hack you might be using.

Now i want to stop my service [service runs a 'timer task'], but when i use stopService() method i am unable to stop my service.

Most likely, your service is already stopped. IntentService stops as soon as onHandleIntent() returns. TimerTask forks a thread to maintain its timer -- if you are creating this TimerTask in onHandleIntent(), you are leaking this thread. You have no way of ever stopping this thread. It will randomly go away once Android elects to terminate your process.

If, on the other hand, you get rid of the TimerTask and use AlarmManager, you can do your real periodic work in the IntentService's onHandleIntent() method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜