Android 作业调度程序。我可以每 15 秒安排一次工作吗
Android JobScheduler. Can I schedule job every 15 seconds
我的代码可以用了。但我不确定这是否会在所有设备上始终有效。
我可以这样使用 JobScheduler 吗?
启动方式:
public static void schedule() {
JobScheduler scheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
JobInfo job = new JobInfo.Builder(1, new ComponentName(context, MySchedulerService.class))
.setMinimumLatency(15000)
.build();
scheduler.schedule(job);
}
服务:
public static class MySchedulerService extends JobService {
public boolean onStartJob(JobParameters params) {
schedule();
/* Business code */
stopSelf();
return false;
}
public boolean onStopJob(JobParameters params) {
return false;
}
}
Can I schedule job every 15 seconds
没有。两个时间表之间的最短持续时间约为 15 分钟。您可以使用 getMinPeriodMillis()
轻松查询此信息
Note:
Attempting to declare a smaller period that this when scheduling a job
will result in a job that is still periodic, but will run with this
effective period.
对于setMinimumLatency
,基于documentation:
Milliseconds before which this job will not be considered for
execution.
这并不意味着作业将在 15 秒后立即执行,并且在 first phase of doze 模式启动时从 Android N 开始不可靠。
我的代码可以用了。但我不确定这是否会在所有设备上始终有效。 我可以这样使用 JobScheduler 吗?
启动方式:
public static void schedule() {
JobScheduler scheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
JobInfo job = new JobInfo.Builder(1, new ComponentName(context, MySchedulerService.class))
.setMinimumLatency(15000)
.build();
scheduler.schedule(job);
}
服务:
public static class MySchedulerService extends JobService {
public boolean onStartJob(JobParameters params) {
schedule();
/* Business code */
stopSelf();
return false;
}
public boolean onStopJob(JobParameters params) {
return false;
}
}
Can I schedule job every 15 seconds
没有。两个时间表之间的最短持续时间约为 15 分钟。您可以使用 getMinPeriodMillis()
轻松查询此信息Note:
Attempting to declare a smaller period that this when scheduling a job will result in a job that is still periodic, but will run with this effective period.
对于setMinimumLatency
,基于documentation:
Milliseconds before which this job will not be considered for execution.
这并不意味着作业将在 15 秒后立即执行,并且在 first phase of doze 模式启动时从 Android N 开始不可靠。