我JobIntentService
在 Nexus 5X (Android 8.0) 上运行来自 Google 的以下示例
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 命令来测试打盹模式
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()
,但稍后继续循环循环。我了解系统会定期退出打盹模式以让应用程序执行延迟活动。在 Doze 模式下服务完成后,运行以下 adb 命令退出 Doze 模式
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 的工作原理