Home > Mobile >  WorkManager not working periodically in Android
WorkManager not working periodically in Android

Time:11-17

I was creating an android notification app which gives notification every 15 minutes after the alarm has been set.

it calls first time, and shows notification but workmanager not calling every 15 minutes.


 val myWorkRequest = PeriodicWorkRequestBuilder<ReminderWorker>(15, TimeUnit.MINUTES).build()

        WorkManager.getInstance(applicationContext)
            .enqueueUniquePeriodicWork("work", ExistingPeriodicWorkPolicy.REPLACE, myWorkRequest)

CodePudding user response:

you need to add initialDelay to the builder

 val myWorkRequest: PeriodicWorkRequest = PeriodicWorkRequest.Builder(
        MySimpleWorker::class.java, 3, TimeUnit.HOURS, 1, TimeUnit.HOURS)
        .setConstraints(constraints)
        .setInitialDelay(2, TimeUnit.HOURS)
        .setBackoffCriteria(BackoffPolicy.LINEAR, 1, TimeUnit.HOURS)
        .build()

CodePudding user response:

Another way would be to use one time request to make this work. you can have one time request to be scheduled for once and when that request is executed and its doWork() is called you can schedule another one for next 15 minutes and then so on like a recursion. thats pretty straight forward.

  • Related