问题描述
我正在尝试使用android Job Scheduler来安排要立即准确执行一次的作业.
I am trying to use android job scheduler to schedule a job to execute immediately and exactly once.
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());
但是它运行了大约3 4倍.这是怎么了?
But it running about 3 4 times. What is wrong here ?
这是工作类别本身:
@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);
}
}
P.S .:我已经检查过我是否每次都完成了false的设置.
P.S.: I have checked that I am callig jobFinished with false value each and every time.
推荐答案
要只运行一次作业,应在后台工作程序的 onStartJob(...)
中运行代码,然后删除调用 onStopJob(...)
时其回调.
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.
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;
}
}
要立即运行它,您应该将最小延迟时间"和替代截止时间"都设置为1.
To run it immediately you should set both "minimum latency" and "override deadline" to 1.
jobScheduler.schedule(new JobInfo.Builder(1,
new ComponentName(context, SyncJobLollipop.class))
.setExtras(bundle)
.setMinimumLatency(1)
.setOverrideDeadline(1)
.build());
请注意,操作系统的打ze"模式可能会将JobScheduler任务推迟到维护周期"或设备以其他某种方式被唤醒之前.但是,以上代码只能在Doze模式的/outside/下运行,因此应几乎无延迟地安排作业.
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. However, the above code would only be running /outside/ of Doze mode, so the job should be scheduled almost without delay.
这篇关于Android Job Scheduler-安排Job立即立即执行一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!