Search code examples
androidandroid-jobscheduler

Android JobScheduler: If you schedule the same job with periodic time, does it start the period again?


What happens if I schedule the same periodic job (same job ID) and the Job has already been scheduled? Does it start its period again from the beginning?

For example, I call this method twice:

JobInfo myLongJob = new JobInfo.Builder(
            JOB_ID,
            new ComponentName(context, JobSchedulerLongService.class.getName())
    ).setPeriodic(10000)
     .build();

    jobScheduler.schedule(myLongJob);

Does scheduling the job second time cause the periodic timer to start counting again?


Solution

  • I found it after doing some tests:

    Does scheduling the job the second time cause the periodic timer to start counting again?

    Yes! and...

    It will depend on:

    • If the job is being executed: Will cause the first job to stop (calling onStopJob method) and start again.
    • If the job is not being executed: Will just start again the countdown.

    Added really useful comment from @Gauthier:

    jobId - int: Application-provided id for this job. Subsequent calls to cancel, or jobs created with the same jobId, will update the pre-existing job with the same id. [link to this doc](http://developer.android.com/reference/android/app/job/JobInfo.Builder.html#JobInfo.Builder(int, android.content.ComponentName))