如何停止 IntentService? [英] How to stop an IntentService?

查看:45
本文介绍了如何停止 IntentService?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 IntentService 来处理我的应用程序中的大文件下载.但是当我想取消下载时,我会调用 stopService(intent).然而,onDestroy() 方法已被调用,但下载并未停止.我还需要做什么才能取消下载?谢谢.

I use an IntentService to handle large file-downloads in my app. But when i want to cancel the download i call stopService(intent). However the onDestroy() method is been called but the download doesn't stop. What else do i have to do to cancel the download? Thanks.

推荐答案

我自己还没搞清楚,但我发送了一个 Intent :

I have not figured this out myself, but I send an Intent with:

intent.putExtra("pause", "yes");
startService(intent);

然后我在 IntentService(不推荐)中覆盖 public int onStartCommand(Intent intent, int a, int b) 并看到暂停"是是"我将静态布尔值 mPaused 设置为 true.如果 onHandleIntent 中有循环,请添加 &&!mPaused 在循环条件中.

And then I override public int onStartCommand(Intent intent, int a, int b) inside the IntentService (which is not recommended) and seeing that "pause" is "yes" I set a static boolean mPaused to true. If you have a loop in onHandleIntent, add && !mPaused in the loop condition.

当然,这都是糟糕"的代码和黑客行为……但我希望其他人能以正确的方式参与其中.

Of course this is all 'bad' code and a hack... but I hope somebody else chimes in with the correct way of doing this.

这篇关于如何停止 IntentService?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆