17

I want to start a JobScheduler at a specific time everyday, and finish it after 3 hours.

I have the part of triggering the job every 20 min, and for 3 hours, but in JobInfo.Builder class there's no option for starting the job at an exact time.

Going over the JobInfo.Builder class overview, there's nothing there that sets the time for starting a JobScheduler.

Obviously, i don't want to run it for the whole day, and check that the time matches, this will drain more battery than needed, and is bad programing.

I was thinking of making an alarm to run at the time i specify and would trigger this job, but this seems to be a bit overkill.

 JobInfo.Builder builder = new JobInfo.Builder(NIGHT_SYNC_JOB_ID, 
 new ComponentName(context, NightSyncDataJobService.class));

 //Job starts at 11pm, and ends at 2am. Running every 20 minutes.
 builder.setPersisted(true); //Job scheduled to work after reboot
 builder.setPeriodic(Consts.ONE_MINUTE * 20);

 JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
 jobScheduler.schedule(builder.build());

If you have any other solution to the issue, beside AlarmManger that triggers this JobScheduler, will be much appreciated.

Dus
  • 3,594
  • 4
  • 22
  • 44

2 Answers2

8

I achieved it in the following way (without AlarmManager), created a new Job (with a unique JOB_ID obviously), and told the JobScheduler to schedule it for sometime in the future using setOverrideDeadline().

Here's a snippet which might be helpful:

private JobInfo getJobInfoForFutureTask(Context context,
                                        long timeTillFutureJob) {
        ComponentName serviceComponent = new ComponentName(context, SchedulerService.class);

        return new JobInfo.Builder(FUTURE_JOB_ID, serviceComponent)
                .setRequiredNetworkType(JobInfo.NETWORK_TYPE_NONE)
                .setOverrideDeadline(timeTillFutureJob)
                .setRequiresDeviceIdle(false)
                .setRequiresCharging(false)
                .setPersisted(true)
                .build();
    }

Make sure to calculate the correct time offset at which you want to schedule the task and also remove any existing Job IDs from the JobScheduler.

prerak
  • 359
  • 4
  • 8
  • Instead of scheduling it daily i want to schedule a job for specific days (lets just say for sunday,wednesday, saturday) ,Is it possible using JobSchedular @prerak – guru_007 Sep 14 '17 at 10:19
  • 1
    Not sure, the only way I can think is you kill the existing job and reschedule a future one with the offset. Need to involve some math in it :D – prerak Sep 18 '17 at 20:59
  • 1
    how do you schedule this job periodically ? like the Op wants every 20 minutes ? As per documentation " it doesn't make sense setting this property on a periodic job, doing so will throw an IllegalArgumentException when build() is called." – kukroid Jul 18 '18 at 00:34
1

adding setMinimumLatency on jobInfo with the deference of the current time and the target time solves this issue.

JobInfo jobInfo = new JobInfo.Builder(1, componentName)
                .setPersisted(true)
                .setBackoffCriteria(6000, JobInfo.BACKOFF_POLICY_LINEAR)
                .setMinimumLatency(1000 * 60)
                .build();

for the example above the scheduler will work after 60 secs.

Omar Alnajjar
  • 317
  • 3
  • 9