[英]Android Job Scheduler - Schedule Job to execute immediately and exactly once
I am trying to use android job scheduler to schedule a job to execute immediately and exactly once. 我正在尝试使用android Job Scheduler来安排要立即立即执行一次的作业。
JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
jobScheduler.cancel(1);
PersistableBundle bundle = new PersistableBundle();
bundle.putInt(JobFlags.KEY_PERIODIC_SYNC_JOB, JobFlags.JOB_TYPE_INITIAL_FETCH);
jobScheduler.schedule(new JobInfo.Builder(1,
new ComponentName(context, SyncJobLollipop.class))
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.setExtras(bundle)
.setMinimumLatency(10)
.setOverrideDeadline(24 * 3600 * 1000)
.build());
But it running about 3 4 times. 但它运行约3 4倍。 What is wrong here ? 这是怎么了
Here is the job class itself: 这是工作类别本身:
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class SyncJobLollipop extends JobService implements JobFinishedListener {
@Inject
SyncJobBackend jobBackend;
private JobParameters jobParameters;
@Override
public boolean onStartJob(JobParameters jobParameters) {
((MyApplication)getApplication()).getAppComponent().inject(this);
this.jobParameters = jobParameters;
PersistableBundle bundle = jobParameters.getExtras();
int type = bundle.getInt(JobFlags.KEY_PERIODIC_SYNC_JOB);
jobBackend.onStartJob(type, this);
return true;
}
@Override
public boolean onStopJob(JobParameters jobParameters) {
jobBackend.onStopJob();
return false;
}
@Override
public void onJobFinished(boolean success) {
jobFinished(jobParameters, !success);
}
}
PS: I have checked that I am callig jobFinished with false value each and every time. PS:我已经检查过我是否每次都用假值完成。
To run the job exactly once, you should run your code in onStartJob(...)
in a background worker, and remove its callbacks when onStopJob(...)
is called. 要只运行一次作业,应在后台工作程序的onStartJob(...)
中运行代码,并在调用onStopJob(...)
时删除其回调。
public class SyncJobLollipop extends JobService {
final Handler workHandler = new Handler();
Runnable workRunnable;
@Override
public boolean onStartJob(JobParameters params) {
workRunnable = new Runnable() {
@Override
public void run() {
// do your work here,
// such as jobBackend.onStartJob(type, this)
boolean reschedule = false;
jobFinished(params, reschedule);
}};
workHandler.post(workRunnable);
return true;
}
@Override
public boolean onStopJob(JobParameters params) {
workHandler.removeCallbacks(workRunnable);
return false;
}
}
To run it immediately you should set both "minimum latency" and "override deadline" to 1. 要立即运行它,您应该将“最小延迟时间”和“替代截止时间”都设置为1。
jobScheduler.schedule(new JobInfo.Builder(1,
new ComponentName(context, SyncJobLollipop.class))
.setExtras(bundle)
.setMinimumLatency(1)
.setOverrideDeadline(1)
.build());
Please note that the operating system's Doze mode might defer JobScheduler tasks until either a "maintenance period" or the device is awoke in some other way. 请注意,操作系统的“打ze”模式可能会将JobScheduler任务推迟到“维护期”或设备以其他某种方式被唤醒之前。 However, the above code would only be running /outside/ of Doze mode, so the job should be scheduled almost without delay. 但是,上面的代码只能在打mode模式的/ outside /下运行,因此应几乎无延迟地安排作业。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.