Search code examples
androidandroid-jobschedulerbootcompleted

Which is better way to re-schedule a job on boot completed?


Job can be rescheduled in 2 ways in android job scheduler.

  1. Using .setPersisted(true)
  2. By scheduling again in BOOT_COMPLETED broadcast receiver.

Which of these is a better way of doing it for periodic jobs?


Solution

  • I wouldn't say there is a better way. It depends on the use case of the job in your app. Both require the RECEIVE_BOOT_COMPLETED uses-permission.

    If a user can turn a periodic task (job) on/off or there is a job with data under constraints waiting and it should persist across reboots, it probably makes sense to use the setPersisted(true) functionality. That way you don't need to worry about doing anything and the job will automatically get loaded from disk upon boot. From my understanding, the job should also be scheduled from where it left off prior to the reboot giving a more uniform scheduling of the job.

    Otherwise, if you don't want this job to be scheduled until the BOOT_COMPLETED intent is received (there can be many reasons for this), then after receiving this intent may be the right place to schedule the job. You also may want to reschedule not only on receiving the BOOT_COMPLETED intent, but other intents such as MY_PACKAGED_REPLACED. All these intents can be received and the jobs scheduled in the same BroadcastReceiver.

    There are most likely other reasons for going with one way or the other.