You can't.
Periodic work has a minimum interval of 15 minutes and it cannot have an initial delay. You can find the proof in the WorkSpec.java
class.
/**
* Sets the periodic interval for this unit of work.
*
* @param intervalDuration The interval in milliseconds
*/
public void setPeriodic(long intervalDuration) {
if (intervalDuration < MIN_PERIODIC_INTERVAL_MILLIS) {
Logger.get().warning(TAG, String.format(
"Interval duration lesser than minimum allowed value; Changed to %s",
MIN_PERIODIC_INTERVAL_MILLIS));
intervalDuration = MIN_PERIODIC_INTERVAL_MILLIS;
}
setPeriodic(intervalDuration, intervalDuration);
}
But there are other ways to deal with that.
- Write unit tests using work-testing library and ensure that your business logic works as expected.
- Use dependency injection approach and provide a
OneTimeWorkRequest
in debug mode, for example:
interface Scheduler {
fun schedule()
}
class DebugScheduler {
fun schedule() {
WorkManager.getInstance().enqueue(
OneTimeWorkRequest.Builder(MyWorker::class.java)
.build()
)
}
}
class ProductionScheduler {
fun schedule() {
// your actual scheduling logic
}
}