Android JobScheduler:如果你用周期时间安排同一个工作,它会重新开始这个周期吗?

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

如果我安排相同的定期作业(相同的作业 ID)并且该作业已被安排,会发生什么情况?它是否从头开始重新开始它的周期?

比如我调用了两次这个方法:

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

    jobScheduler.schedule(myLongJob);

第二次调度作业是否会导致周期性计时器再次开始计数?

经过测试发现:

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

是的!和...

这将取决于:

  • 如果作业正在执行:会导致第一个作业停止(调用onStopJob方法)并重新开始。
  • 如果作业未被执行:将重新开始倒计时。

添加了来自@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))