如何禁止IntentService?IntentService

2023-09-04 11:43:50 作者:ー個亽の江湖

我用一个IntentService处理大型文件的下载我的应用程序。但是,当我想取消下载我叫 stopService(意向)。然而,的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.

推荐答案

我还没有想通了这一点,不过我发意图

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

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

然后我重写公众诠释onStartCommand(意向意图,诠释一个,int b)在 IntentService (这是不推荐),看到了暂停是是我设置静态布尔 mPaused 。如果您在 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.

当然,这一切都是'坏'code和黑客攻击......但我希望别人编钟在这样做的正确方法。

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