我想创建一个在两个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