Java 安卓系统提供停止服务的意图

Java 安卓系统提供停止服务的意图,java,android,rx-java2,Java,Android,Rx Java2,我有一个服务,它根据onStartCommand中的intent发送一些Completable,并在所有启动Completable完成后调用stopSelf 我还在ondestory中处置所有未完成的文件,以清除所有内容 import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; import android.util.SparseAr

我有一个服务,它根据
onStartCommand
中的
intent
发送一些
Completable
,并在所有启动
Completable
完成后调用
stopSelf

我还
ondestory
中处置
所有未完成的文件,以清除所有内容

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.util.SparseArray;

import androidx.annotation.Nullable;

import io.reactivex.Completable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public abstract class RxIntentService extends Service {

    private static final String TAG = "RxIntentService";

    private SparseArray<Disposable> pendingCommands;

    @Override
    public void onCreate() {
        super.onCreate();
        pendingCommands = new SparseArray<>();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand: " + getClass().getSimpleName() + ", " + startId);
        Completable completable = handleIntent(intent);
        if (completable == null) {
            commandCompleted(startId);
            return START_NOT_STICKY;
        } else {
            pendingCommands.append(startId,
                    completable
                            .subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(
                                    () -> commandCompleted(startId),
                                    throwable -> {
                                        Log.w(TAG, getClass().getSimpleName() + " command failed: " + (intent == null ? "intent is null" : intent.getAction()), throwable);
                                        commandCompleted(startId);
                                    })
            );
            return START_REDELIVER_INTENT;
        }
    }

    private void commandCompleted(int startId) {
        Log.d(TAG, "commandCompleted: " + getClass().getSimpleName() + ", " + startId);
        pendingCommands.remove(startId);
        if (pendingCommands.size() == 0) {
            Log.d(TAG, "commandCompleted: will stop self");
            stopSelf();
        }
    }

    @Override
    public void onDestroy() {
        Log.d(TAG, "onDestroy: " + getClass().getSimpleName());
        for (int i = 0; i < pendingCommands.size(); i++) {
            pendingCommands.valueAt(i).dispose();
        }
        pendingCommands.clear();
        pendingCommands = null;
        super.onDestroy();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    protected abstract Completable handleIntent(Intent intent);
}

我如何防止这种行为?我可以在
onStartCommand
中检查它是否即将被销毁,但是我如何处理这个特定的意图呢?

似乎我需要使用
stopSelfResult(startId)
而不是
stopSelf()
,如下所述:

如果服务最近启动的时间是startId,则停止服务。这与为此特定服务调用stopService(Intent)相同,但允许您在onStart(Intent,int)中尚未看到来自客户端的启动请求时安全地避免停止


它直接解决了我在
onStartCommand

中没有观察到的意图的问题,这个类的用例是什么?您的问题暗示它的使用方式类似于
IntentService
,但
IntentService
用于在UI线程下执行一些简短的工作,这让我想知道您想在哪里/为什么只在调用站点使用Rx来使用它。@p在用例中,用户选择配置文件图片,我在后台同步它。为什么不在活动/片段中处理它?用户可以在选择图片后按home键,它将被放置在顶部的
上。此外,由于网络连接不良,可能需要几秒钟以上的时间。所以我认为使用服务会更好。
D/RxIntentService: onStartCommand: ProfileService, 1
D/RxIntentService: onStartCommand: ProfileService, 2
D/RxIntentService: commandCompleted: ProfileService, 1
D/RxIntentService: commandCompleted: ProfileService, 2
D/RxIntentService: commandCompleted: will stop self
D/RxIntentService: onStartCommand: ProfileService, 3
D/RxIntentService: onDestroy: ProfileService