Android JobScheduler onStartJob多次调用 [英] Android JobScheduler onStartJob called multiple times
问题描述
尽管作业已完成,JobScheduler会多次调用onStartJob()
.如果我只安排一项工作,然后等到完成,一切都会很好.但是,如果我同时调度两个或多个具有不同ID的作业,则在调用jobFinished()
之后会再次调用onStartJob()
.
The JobScheduler calls onStartJob()
multiple times, although the job finished. Everything works fine, if I schedule one single job and wait until it has finished. However, if I schedule two or more jobs with different IDs at the same time, then onStartJob()
is called again after invoking jobFinished()
.
例如,我计划作业1和作业2使用除ID之外的完全相同的参数,则顺序为:
For example I schedule job 1 and job 2 with exactly the same parameters except the ID, then the order is:
-
onStartJob()
用于作业1和作业2 - 这两个作业都完成了,所以都为它们调用了
jobFinished()
- 之后,对于具有相同ID的两个作业,再次调用
onStartJob()
onStartJob()
for job 1 and job 2- Both jobs finish, so
jobFinished()
is invoked for both of them - After that
onStartJob()
is called again for both jobs with the same ID
我的工作很基础,并不复杂.
My job is very basic and not complicated.
public class MyJobService extends JobService {
@Override
public boolean onStartJob(final JobParameters params) {
new Thread(new Runnable() {
@Override
public void run() {
try {
// do something
} finally {
// do not reschedule
jobFinished(params, false);
}
}
}).start();
// yes, job running in the background
return true;
}
@Override
public boolean onStopJob(JobParameters params) {
// mark my background task as stopped
// do not reschedule
return false;
}
}
我安排这样的工作
JobInfo jobInfo = createBaseBuilder(request)
.setMinimumLatency(2_000L)
.setOverrideDeadline(4_000L)
.setRequiresCharging(false)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.build();
int scheduleResult = mJobScheduler.schedule(jobInfo);
// is always success
我不知道怎么了.
推荐答案
我猜这是由待处理的Job引起的,因此在服务启动后我调用了mJobScheduler.cancelAll(),问题已解决.
I guess it's caused by the pending Job, so I call mJobScheduler.cancelAll() after the service started, problem resolved.
这篇关于Android JobScheduler onStartJob多次调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!