我在Nexus 5X(Android 8.0)上使用Google运行以下JobIntentService
示例
public class SimpleJobIntentService extends JobIntentService {
/**
* Unique job ID for this service.
*/
static final int JOB_ID = 1000;
/**
* Convenience method for enqueuing work in to this service.
*/
static void enqueueWork(Context context, Intent work) {
enqueueWork(context, SimpleJobIntentService.class, JOB_ID, work);
}
@Override
protected void onHandleWork(Intent intent) {
// We have received work to do. The system or framework is already
// holding a wake lock for us at this point, so we can just go.
Log.i("SimpleJobIntentService", "Executing work: " + intent);
String label = intent.getStringExtra("label");
if (label == null) {
label = intent.toString();
}
toast("Executing: " + label);
for (int i = 0; i < 5; i++) {
Log.i("SimpleJobIntentService", "Running service " + (i + 1)
+ "/5 @ " + SystemClock.elapsedRealtime());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
Log.i("SimpleJobIntentService", "Completed service @ " + SystemClock.elapsedRealtime());
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(getClass().toString(), "JobIntentService destroyed");
}
}
该服务已添加到AndroidManifest.xml,如下所示。
<service android:name=".SimpleJobIntentService "
android:exported="true"
android:permission="android.permission.BIND_JOB_SERVICE"/>
在正常情况下,服务按预期运行,在退出之前循环循环。
现在我运行了以下adb命令来测试Doze模式
adb shell dumpsys deviceidle force-idle
修改 以下是服务启动时然后强制进入打盹模式的日志
12-14 12:11:18.587 24357-24396/<package-name> I/SimpleJobIntentService: after sleep
12-14 12:11:18.588 24357-24396/<package-name> I/SimpleJobIntentService: Running service 2/5 @ 510158341
12-14 12:11:18.588 24357-24396/<package-name> I/SimpleJobIntentService: before sleep
12-14 12:11:19.629 24357-24396/<package-name> I/SimpleJobIntentService: after sleep
12-14 12:11:19.629 24357-24396/<package-name> I/SimpleJobIntentService: Running service 3/5 @ 510159383
12-14 12:11:19.629 24357-24396/<package-name> I/SimpleJobIntentService: before sleep
12-14 12:11:20.593 24357-24357/<package-name> I/SimpleJobIntentService: JobIntentService destroyed
12-14 12:11:20.670 24357-24396/<package-name> I/SimpleJobIntentService: after sleep
12-14 12:11:20.670 24357-24396/<package-name> I/SimpleJobIntentService: Running service 4/5 @ 510160424
12-14 12:11:20.670 24357-24396/<package-name> I/SimpleJobIntentService: before sleep
12-14 12:11:21.708 24357-24396/<package-name> I/SimpleJobIntentService: after sleep
12-14 12:11:21.709 24357-24396/<package-name> I/SimpleJobIntentService: Running service 5/5 @ 510161463
12-14 12:11:21.709 24357-24396/<package-name> I/SimpleJobIntentService: before sleep
12-14 12:11:22.750 24357-24396/<package-name> I/SimpleJobIntentService: after sleep
12-14 12:11:22.750 24357-24396/<package-name> I/SimpleJobIntentService: Completed service @ 510162504
它清楚地调用onDestroy()
,但稍后会继续循环循环。我知道系统会定期退出打盹模式,让应用程序执行延迟活动。在打盹模式下完成服务后,将运行以下adb命令以退出打盹模式
adb shell dumpsys deviceidle unforce
修改 以下是执行上述adb命令时的日志
12-14 12:15:25.244 24357-24481/<package-name> I/class SimpleJobIntentService : work started
12-14 12:15:25.244 24357-24481/<package-name> I/SimpleJobIntentService: Running service 1/5 @ 510404998
12-14 12:15:25.244 24357-24481/<package-name> I/SimpleJobIntentService: before sleep
12-14 12:15:26.285 24357-24481/<package-name> I/SimpleJobIntentService: after sleep
12-14 12:15:26.285 24357-24481/<package-name> I/SimpleJobIntentService: Running service 2/5 @ 510406039
12-14 12:15:26.285 24357-24481/<package-name> I/SimpleJobIntentService: before sleep
12-14 12:15:27.326 24357-24481/<package-name> I/SimpleJobIntentService: after sleep
12-14 12:15:27.327 24357-24481/<package-name> I/SimpleJobIntentService: Running service 3/5 @ 510407080
12-14 12:15:27.327 24357-24481/<package-name> I/SimpleJobIntentService: before sleep
12-14 12:15:28.367 24357-24481/<package-name> I/SimpleJobIntentService: after sleep
12-14 12:15:28.368 24357-24481/<package-name> I/SimpleJobIntentService: Running service 4/5 @ 510408121
12-14 12:15:28.368 24357-24481/<package-name> I/SimpleJobIntentService: before sleep
12-14 12:15:29.408 24357-24481/<package-name> I/SimpleJobIntentService: after sleep
12-14 12:15:29.409 24357-24481/<package-name> I/SimpleJobIntentService: Running service 5/5 @ 510409163
12-14 12:15:29.409 24357-24481/<package-name> I/SimpleJobIntentService: before sleep
12-14 12:15:30.446 24357-24481/<package-name> I/SimpleJobIntentService: after sleep
12-14 12:15:30.446 24357-24481/<package-name> I/SimpleJobIntentService: Completed service @ 510410200
12-14 12:15:30.457 24357-24357/<package-name> I/SimpleJobIntentService: JobIntentService destroyed
JobIntentService虽然早先完成,但会再次执行。这是可接受的行为吗?或者我错过了什么?帮助我理解JobIntentService的工作