I am trying to use the Task Scheduler for Android to schedule a task to run 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());
But it works about 3 times. What is wrong here?
Here is the task 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 checked that I execute a callig job with a false value every time.
source
share