
build() val work = PeriodicWorkRequestBuilder(1, TimeUnit.HOURS). In this case, we are adding a charging constraint to the PeriodicWorkRequest and we are enqueueing it: val constraints = Constraints.Builder(). PeriodicWorkRequest with a charging constraints For example, if you specify that the Work only runs when the device is charging, even if your minimum interval passes, if the device is not charging, this Work will not run until the device is plugged in. It’s called the minimum interval because of Android’s battery optimizations and because you can have constraints that extend the time between repetitions. We have an additional parameter that specifies the minimum repeat interval: val work = PeriodicWorkRequestBuilder(1, TimeUnit.HOURS).

The call to build a periodic work request is not very different from the one we’ve seen for the one time work. Plus we will cover a couple of ways to write tests for your Worker classes. In this article I’ll cover the basics of periodic work, some common use cases and some common errors. Some of the most common questions I get at conferences are around periodic work. Before v2.1-alpha02 it’s not possible to create a PeriodicWorkRequest with an initial delay.Worker classes cannot be chained in a PeriodicWorkRequest.Minimum period length is 15 minutes (same as JobScheduler).In a previous blog, we’ve seen that you can schedule work using a OneTimeWorkRequest but if you want that your work is repeated periodically, you can use a PeriodicWorkRequest.įirst, let’s see what are the differences between these two types of WorkRequest: WorkManager’s Kotlin extensions and CoroutineWorker.How to use the WorkManager API to schedule Work.What WorkManager is and when to use WorkManager.If you’re been following thus far, we’ve talked about:

It is the current best practice for most background work on Android. WorkManager is an Android Jetpack library that makes it easy to schedule deferrable, asynchronous tasks that must be run reliably. More on that here.Welcome to the fourth post of our WorkManager series. If we set an interval less than 15 minutes, that will get overridden to 15 minutes in runtime. Note that the default minimum interval is 15 minutes and that is android default. Class UpdateLocationWorker ( context : Context, workerParams : WorkerParameters, private val someRepository : SomeRepository ) : CoroutineWorker ( context, workerParams )
