Jobscheduler API android L

2.8k views Asked by At

I am making an application that makes use of the jobscheduler API. I want to run a service periodically and when the device is charged. This is the code.

JobInfo.Builder builder = new JobInfo.Builder(kJobId++, mServiceComponent);
                    builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
                    builder.setPeriodic(3000);
                    builder.setRequiresCharging(true);
                    mTestService.scheduleJob(builder.build());

Now when I run this and I unplug the device, the service still runs after 3 secs. There is no effect of setting the setRequiresCharging.

When i comment out builder.setPeriodic(3000), it works perfectly fine. I am not sure as to where I am going wrong.

1

There are 1 answers

4
adneal On BEST ANSWER

To identify each job internally, the framework creates a new JobStatus when a new job lands on the scheduler. One of the first things JobStatus does is to check and see if your job has a periodic update interval. If it does, it uses the periodic update interval to determine the latest point in the future that a job must be run, or in other words, if it has a deadline constraint.

One of the criteria for a job to be considered ready to be executed, it that all of its constraints have been satisfied or the deadline of a job has expired. See JobStatus.isReady for more information.

The JobSchedulerService adds several StateControllers used to track when jobs should run and when they must be stopped. One of these controllers is a TimeController, which:

sets an alarm for the next expiring job, and determines whether a job's minimum delay has been satisfied

To determine if a new alarm should be scheduled, TimeController checks if your job has any time delay or deadline constraints, which all jobs seem to be given if you set a periodic update interval.

I hope that helps you at least understand why your job continues being scheduled despite your battery constraint. But I don't have a solution that can offer a straightforward fix for this at the moment.