JobIntentService не перезапускается после уничтожения системой - PullRequest
0 голосов
/ 22 марта 2019

Я хочу создать службу, которая работает в фоновом режиме на обоих android 8, поэтому я использую JobIntentService.

public class BatteryStateService extends JobIntentService {

    static final int JOB_ID = 5904;

    private static boolean isLive = false;
    public BatteryChargeStateReceiver mReceiver;

    public static void startService(Context context, Intent work) {
        enqueueWork(context, BatteryStateService.class, JOB_ID, work);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        isLive = true;
        mReceiver = BatteryChargeStateReceiver.register(this);
        if (PrefUtils.isShowStatusBar(this)){
            startForeground(2, MyNotificationManager.getInstance(this).init());
        }
    }

    @Override
    public void onDestroy() {
        isLive = false;
        if (mReceiver != null){
            BatteryChargeStateReceiver.unRegister(this, mReceiver);
            mReceiver = null;
        }
        BatteryStateService.startService(this, new Intent(this, BatteryStateService.class));
        super.onDestroy();
    }

    @Override
    protected void onHandleWork(@NonNull Intent intent) {
        if (intent != null && intent.getAction() != null){
            if (intent.getAction().equals(Config.ACTION_STOP_FOREGROUND_NOTIFICATION)) {
                stopForeground(true);
            } else if (intent.getAction().equals(Config.ACTION_START_FOREGROUND_NOTIFICATION)) {
                startForeground(2, MyNotificationManager.getInstance(this).init());
            }
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }
....

Однако мое приложение было убито, как только я нажал кнопку «Домой».Как мне сохранить мой сервис?Это журнал:

03-22 09:48:50.977 8637-8726/com.xxxx.xxxx D/ConnectivityManager.CallbackHandler: CM callback handler got msg 524296
03-22 09:48:53.777 925-1260/? I/WindowState: WIN DEATH: Window{3982b3c8 u0 com.tohsoft.fastcharger/com.xxxx.xxxx.main.MainActivity}
03-22 09:48:53.787 925-2545/? I/ActivityManager: Process com.xxxx.xxxx (pid 8637) has died
03-22 09:48:53.797 925-2545/? D/ActivityManager: cleanUpApplicationRecordLocked app:ProcessRecord{286740c6 8637:com.xxxx.xxxx/u0a464}, restarting:false, allowRestart:true, index:-1
03-22 09:48:53.797 925-2545/? D/OppoProcessManager: ProcessRecord{286740c6 8637:com.xxxx.xxxx/u0a464} died but not restart......
03-22 09:48:53.797 925-2545/? D/ActivityManager: cleanUpApplicationRecordLocked:goodbye proc com.xxxx.xxxx
...