【问题标题】:How to stop an IntentService?如何停止 IntentService?
【发布时间】:2011-03-31 16:37:19
【问题描述】:
我使用 IntentService 来处理我的应用程序中的大型文件下载。但是当我想取消下载时,我打电话给stopService(intent)。然而,onDestroy() 方法被调用,但下载并没有停止。
我还需要做什么才能取消下载?
谢谢。
【问题讨论】:
标签:
android
service
download
【解决方案1】:
我自己还没有弄清楚,但我发送了Intent 并附有:
intent.putExtra("pause", "yes");
startService(intent);
然后我在IntentService 中覆盖public int onStartCommand(Intent intent, int a, int b)(不推荐),看到“暂停”为“是”,我将静态布尔值mPaused 设置为true。如果onHandleIntent 中有循环,请在循环条件中添加&& !mPaused。
当然,这都是“糟糕”的代码和黑客行为……但我希望其他人能以正确的方式加入。