WorkManager not working periodically in Android
Asked Answered
L

3

1

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)

Longford answered 17/11, 2022 at 3:9 Comment(8)
Use AlarmManager instead of WorkManager.Oshaughnessy
but it's not running background. are you sure alarm manager is needed? I already tried with it but it didn't work. I just want to show notification every 15 minutes , data from database.Longford
I used alarm manager + broadcastreceiver , - android 11Longford
Clone and run this repo you'll get it- github.com/alamincmt/android (SimpleAlarmClock-Demo Project)Oshaughnessy
@Oshaughnessy thanks. I will try, but still in doubt. because android 11 has any restrictions to make it work?Longford
you need to add initialDelay to the build .setInitialDelay(15, TimeUnit.MINUTE) something like thisBaerl
is there any kotlin versionLongford
follow this https://mcmap.net/q/584660/-workmanager-not-working-periodically-in-androidHamster
H
1

After many days i found a solutions for periodic work manager Use time intervals of at least 1 hour. In document mentions minimum 15 minutes but it is not working don't why not mention in documents when i set 1 hours then it working.

  fun scheduleNotificationRepeated() {
    val constraints: Constraints = Constraints.Builder().apply {
        setRequiredNetworkType(NetworkType.CONNECTED)
        setRequiresBatteryNotLow(true)
    }.build()
    val request: PeriodicWorkRequest = PeriodicWorkRequest.Builder(
        NotifyWork::class.java, 1, TimeUnit.HOURS, 1, TimeUnit.HOURS)
        .setConstraints(constraints)
        .setInitialDelay(2, TimeUnit.HOURS)
        .setBackoffCriteria(BackoffPolicy.LINEAR, 1, TimeUnit.HOURS)
        .build()
    val instanceWorkManager = WorkManager.getInstance(this)
        instanceWorkManager.enqueueUniquePeriodicWork(
        "TAG_WORK_NAME",
        ExistingPeriodicWorkPolicy.KEEP,
        request
    )
}
Hamster answered 10/10, 2023 at 9:14 Comment(1)
Not working for me, using 8 hours delay with 30 minutes flex. Tested on Android 10 and 12Electromechanical
J
0

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.

Jerrylee answered 17/11, 2022 at 6:37 Comment(0)
B
-1

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()
Baerl answered 17/11, 2022 at 3:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.