2

How to reschedule JobScheduler that I started with setPeriodic(), I want to change the scheduler time later with user input.

JobInfo.Builder builder =
    new JobInfo.Builder(JOB_ID, new ComponentName(this, MyJobScheduler.class));
builder
   .setPeriodic(15000)
   .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
   .setPersisted(true);
jobScheduler.schedule(builder.build());
Vlad
  • 18,195
  • 4
  • 41
  • 71
katwal-Dipak
  • 3,523
  • 2
  • 23
  • 23

2 Answers2

3

Currently, there is no option to reschedule a job. What you can do is call the cancel() method to cancel the job with the given job id and schedule a new job. So it would look something like:

jobScheduler.cancel(JOB_ID);
// Construct a new JobInfo.Builder
jobScheduler.schedule(builder.build());
ucsunil
  • 7,378
  • 1
  • 27
  • 32
  • So, Can I Start Another JobScheduler From Current JobScheduler and Cancel previous One? – katwal-Dipak Apr 12 '15 at 06:05
  • Yes, you can. Or you can use a new one. It doesn't matter as the JobScheduler instance you are creating is really hooking into the system service JOB_SCHEDULER_SERVICE. All instances will be hooking on to this. Hence as long as you supply an id (of an existing job), it will identify the one you want to cancel correctly – ucsunil Apr 12 '15 at 06:11
  • How do I Check jobScheduler with particular ID is Currently Running or not? – katwal-Dipak Apr 12 '15 at 07:05
  • You can do getAllPendingJobs() to return a list of JobInfo objects currently. From there, you can get the job id by iterating through the list and calling the getId() object. That is the most you can do at this time. You can figure out from there what you want - if a job matches a given id, then do some action. – ucsunil Apr 12 '15 at 10:09
3

If you want to change the parameter later, you can do exactly as you wrote. However, you must use exactly the same job id. The system will update the job.

greywolf82
  • 21,813
  • 18
  • 54
  • 108