API 15+上的ForegroundService每15秒杀死一次并重新创建

时间:2018-12-27 05:51:15

标签: android-8.0-oreo android-wake-lock foreground-service powermanager

我有一个具有计时器任务的服务。 这是她工作的日志(请参阅秒):

2018-12-26 11:03:40.746 D/TESTT: TEST 1__
2018-12-26 11:03:41.746 D/TESTT: TEST 1__
2018-12-26 11:03:58.009 D/TESTT: TEST 1__
2018-12-26 11:03:58.994 D/TESTT: TEST 1__
2018-12-26 11:03:59.993 D/TESTT: TEST 1__
2018-12-26 11:04:00.993 D/TESTT: TEST 1__
2018-12-26 11:04:01.994 D/TESTT: TEST 1__
2018-12-26 11:04:02.993 D/TESTT: TEST 1__
2018-12-26 11:04:03.993 D/TESTT: TEST 1__
2018-12-26 11:04:20.229 D/TESTT: TEST 1__
2018-12-26 11:04:21.228 D/TESTT: TEST 1__
2018-12-26 11:04:22.227 D/TESTT: TEST 1__
2018-12-26 11:04:23.227 D/TESTT: TEST 1__
2018-12-26 11:04:24.228 D/TESTT: TEST 1__
2018-12-26 11:04:25.228 D/TESTT: TEST 1__
2018-12-26 11:04:26.232 D/TESTT: TEST 1__
2018-12-26 11:04:43.448 D/TESTT: TEST 1__
2018-12-26 11:04:44.448 D/TESTT: TEST 1__
2018-12-26 11:04:45.448 D/TESTT: TEST 1__
2018-12-26 11:04:46.449 D/TESTT: TEST 1__
2018-12-26 11:04:47.449 D/TESTT: TEST 1__
2018-12-26 11:04:48.450 D/TESTT: TEST 1__
2018-12-26 11:04:49.450 D/TESTT: TEST 1__
2018-12-26 11:05:05.525 D/TESTT: TEST 1__
2018-12-26 11:05:06.520 D/TESTT: TEST 1__
2018-12-26 11:05:07.520 D/TESTT: TEST 1__
2018-12-26 11:05:08.521 D/TESTT: TEST 1__
2018-12-26 11:05:09.521 D/TESTT: TEST 1__
2018-12-26 11:05:10.521 D/TESTT: TEST 1__
2018-12-26 11:05:11.522 D/TESTT: TEST 1__
2018-12-26 11:05:27.594 D/TESTT: TEST 1__
2018-12-26 11:05:28.590 D/TESTT: TEST 1__
2018-12-26 11:05:29.591 D/TESTT: TEST 1__
2018-12-26 11:05:30.590 D/TESTT: TEST 1__
2018-12-26 11:05:31.590 D/TESTT: TEST 1__
2018-12-26 11:05:32.594 D/TESTT: TEST 1__
2018-12-26 11:05:33.590 D/TESTT: TEST 1__
2018-12-26 11:05:50.670 D/TESTT: TEST 1__
2018-12-26 11:05:51.667 D/TESTT: TEST 1__
2018-12-26 11:05:52.667 D/TESTT: TEST 1__
2018-12-26 11:05:53.668 D/TESTT: TEST 1__
2018-12-26 11:05:54.667 D/TESTT: TEST 1__
2018-12-26 11:05:55.667 D/TESTT: TEST 1__
2018-12-26 11:05:56.670 D/TESTT: TEST 1__

我检查了android数据并确定了我的服务的优先级:

Proc # 1: prcp  F/S/FGS  trm: 0 6094:com.package.app/u0a345 (fg-service)

服务由startForegroundService(...启动。 我的服务通知设置为:

.setOngoing(true)
...
startForeground(NOTIFICATION_FOREGROUND, builderForegroung.build());

还添加了WakeLock:

public void setWakeMode(Context context, int mode) {
    Log.d("MAIN_SERVICE", "TEST: SET WAKEMODE");
    boolean wasHeld = false;
    if (wakeLock != null) {
        if (wakeLock.isHeld()) {
            wasHeld = true;
            wakeLock.release();
        }
        wakeLock = null;
    }
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(mode | PowerManager.ON_AFTER_RELEASE, MainService.class.getName());
    wakeLock.setReferenceCounted(false);
    if (wasHeld) {
        wakeLock.acquire();
    }
}

我不需要任何JobService e.t.c.因为服务具有简单的逻辑。

0 个答案:

没有答案