如何强制停止正在进行意向服务?正在进行、意向

2023-09-12 22:53:25 作者:白鸥掠海

我有一个目的服务,下载的视频数千兆字节。我有一个停止按钮,停止下载,如果不小心点击启动或什么的。我知道这已经被问了几次,但对我来说没有任何工作的答案。

我尝试调用 stopService(),不能正常工作。这只是调用 IntentService.OnDestroy()。 席力图召 stopSelf()的onDestroy ,也不管用。

我想有类似的标志,但onHandleIntent不会被调用,如果它已经运行,要等到目前的工作已完成,并执行即可。即使这一切都工作过,我就必须有像一个巨大的if语句,该吸

是我唯一的选择真的把它改写为常规服务?

//答案

 公共类SyncService扩展IntentService {

    布尔isCanceled;

    @覆盖
    公众诠释onStartCommand(意向意图,诠释标志,诠释startId){

        如果(intent.hasExtra(行动)){

            //设置取消标志
            isCanceled = intent.getStringExtra(行动)等于(取消)。

        }
        返回super.onStartCommand(意向,标志,startId);
    }

    @覆盖
    保护无效onHandleIntent(意向意图){

        //清理可能队列
        如果(intent.hasExtra(行动)){
            布尔取消= intent.getStringExtra(行动)。等于(取消);
            如果(取消){
                返回;
            }
        }

        ...

        让你的InputStream从HttpURLConnection的或任何

        ...

        而((读取动作= in.read(缓冲液))大于0){
            如果(isCanceled){
                isCanceled = FALSE;
                打破;
            }

            ...
        }

    }
}
 

和与触发它

 意向意图=新的意图(背景下,SyncService.class);
intent.putExtra(行动,取消);
context.startService(意向);
 

解决方案 供电系统改造 西安住建局部分业务系统11月13日至16日暂停服务

您有两个不同的问题,我会想:

如何停止当前下载

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

第一个是将不得不成为就像一个标志,你看看,你下载的数据。否则,没有什么会阻止你的下载操作。如果您使用的是典型的的HttpURLConnection 配方,你检查的标志在你的循环,你从HTTP 的InputStream 和阅读写信给你的的FileOutputStream 。您可以设置通过调用该标志为 startService()与特定意图结构,确定它作为一个取消操作。你可能需要重写 onStartCommand() IntentService ,看看意图,用它来设置的标志,如果是取消意图,或链条的超类的任何其他种类的意图

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

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.

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

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?

//Answer

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;
            }

            ...
        }

    }
}

And trigger it with

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

解决方案

You have two separate issues, I would think:

How to stop the current download

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

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.

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.