如何强制停止正在进行的意图服务? [英] How to force stop Intent Service in progress?

查看:17
本文介绍了如何强制停止正在进行的意图服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Intent 服务,可以下载数 GB 的视频.我有一个停止"按钮,如果不小心点击开始"或其他任何东西,可以停止下载.我知道这已经被问过几次了,但没有对我有用的答案.

I have an intent service which downloads several gigabytes of videos. I have a "Stop" button, to stop the download if accidentally hit "Start" or whatever. I know this has been asked a couple of times but with no working answer for me.

我尝试调用 stopService(),不起作用.这只是调用 IntentService.OnDestroy().我试图在 onDestroy 内调用 stopSelf(),也不起作用.

I try to call stopService(), doesn't work. That just calls IntentService.OnDestroy(). I tried to call stopSelf() inside onDestroy, doesn't work either.

我尝试使用标志之类的东西,但是如果 onHandleIntent 已经在运行,则不会被调用,它会等待当前工作完成并执行.即使这会奏效,我也必须有类似巨大的 if 语句之类的东西,这很糟糕

I tried to have something like a flag, but onHandleIntent doesn't get called if its already running, it waits till current work is finished and executes then. And even if this would have worked, I would have to have something like a giant if statement, that sucks

我真的唯一的选择是将其重写为常规服务吗?

Is my only option really to rewrite it to a regular Service?

//答案

public class SyncService extends IntentService {

    boolean isCanceled;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        if (intent.hasExtra("action")) {

            // Set the canceling flag
            isCanceled= intent.getStringExtra("action").equals("cancel");

        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        // Clean up the possible queue
        if (intent.hasExtra ("action")) {
            boolean cancel = intent.getStringExtra ("action"). Equals ("cancel");
            if (cancel) {
                return;
            }
        }

        ...

        Get your inputStream from HttpUrlConnection or whatever

        ...

        while ((bytesRead = in.read(buffer)) > 0) {
            if (isCanceled) {
                isCanceled = false;
                break;
            }

            ...
        }

    }
}

并用

Intent intent = new Intent(context, SyncService.class);
intent.putExtra("action", "cancel");
context.startService(intent);

推荐答案

你有两个不同的问题,我认为:

You have two separate issues, I would think:

  1. 如何停止当前下载

  1. How to stop the current download

如何停止排队下载,应在当前下载完成后执行

How to stop queued up downloads, that should execute after the current one completes

第一个必须是类似于标志的东西",您在下载数据时检查.否则,什么都不会阻止您的下载操作.如果您使用的是典型的 HttpUrlConnection 配方,则在从 HTTP InputStream 读取并写入 FileOutputStream 的循环中检查该标志.您可以通过调用具有特定 Intent 结构的 startService() 设置该标志,将其标识为取消"操作.您需要在 IntentService 中覆盖 onStartCommand(),查看 Intent,如果它是取消,则使用它来设置标志 Intent,或链接到任何其他类型的 Intent 的超类.

The first one is going to have to be "something like a flag", that you check as you download the data. Otherwise, nothing is going to stop your download operation. If you are using a typical HttpUrlConnection recipe, you check that flag in your loop where you read from the HTTP InputStream and write to your FileOutputStream. You set that flag via a call to startService() with a particular Intent structure, identifying it as a "cancel" operation. You would need to override onStartCommand() in your IntentService, look at the Intent, use it to set the flag if it is the cancel Intent, or chain to the superclass for any other sort of Intent.

如果您可能有其他命令排队(场景 #2),您还需要检查 onHandleIntent() 顶部的标志.

If you also may have other commands queued up (scenario #2), you would need to check that flag at the top of onHandleIntent() as well.

这篇关于如何强制停止正在进行的意图服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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