Interview frequency: …
Short answer
OneTimeWorkRequest runs the task only once, while PeriodicWorkRequest runs it periodically and repeatedly, and the minimum interval is 15 minutes to save power.
Explanation
The WorkManager library imposes a minimum of 15 minutes for the PeriodicWorkRequest interval (the constant PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS) to avoid draining the device's resources and battery. Periodic tasks also support a flex period that determines when the system may run the task within each repeat cycle according to the specified conditions, and batching system tasks at the same time.
Code example
// A periodic task runs at most once every six hours (the interval is a minimum, not an exact schedule):
val syncRequest = PeriodicWorkRequestBuilder<SyncWorker>(
repeatInterval = 6,
repeatIntervalTimeUnit = TimeUnit.HOURS,
flexTimeInterval = 30, // 30-minute flex window
flexTimeIntervalUnit = TimeUnit.MINUTES
).build()
WorkManager.getInstance(context).enqueueUniquePeriodicWork(
"SyncWork",
ExistingPeriodicWorkPolicy.KEEP,
syncRequest
)Common mistakes
- Trying to set repeatInterval to less than 15 minutes, where WorkManager ignores the value and forcibly resets it to 15 minutes as a minimum automatically.