IntentService onHandleIntent() still running after onDestroy() fired

非 Y 不嫁゛ 提交于 2019-12-06 02:46:58

Finally figured out how to make it work.

As I stated in my question, somehow onHandleIntent() will create a thread to do the job. So even when the service itself is destoryed, the thread is still running. I achieved my goal by adding a global variable

private static boolean isStopped = false;

to DownloadService class.

In order to cancel my service, instead of calling

Setting.this.stopService(new Intent(Setting.this, DownloadService.class));

just set DownloadService.isStopped = true.

Finally, while doing things in onHandleIntent(), check this boolean periodically to see if it should stop downloading. If isStopped = true, return immediately and the service will stop itself.

Hope this helps someone who come across this problem too. And thanks for your time reading this question.

It has a separate thread to do work, and depending on what it is doing it might not be possible to stop it immediately. If it is blocking on I/O, interrupting it will likely have no effect.

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