I have built a simple PeriodicWorkRequest following Android's WorkManager docs here. I basically need a worker that runs every 15 minutes. However my worker is only running once and never again.
Here is how I start the work request:
PeriodicWorkRequest myWorkRequest =
new PeriodicWorkRequest.Builder(
MyWorker.class,
PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS,
TimeUnit.MILLISECONDS
).build();
WorkManager.getInstance(this).enqueue(myWorkRequest);
My worker class is nothing but only a system log inside, just to test it. Here is the related part of the worker:
@Override
public Result doWork() {
Log.d("Worker runs");
return Result.success();
}
I am expecting this worker to run every 15 minutes. However, it only runs once at the launching of the app, and never again.
I have searched a lot and noticed a few things about it, but still no luck:
Time interval is important and has to be minimum 15 minutes as also answered here. However, my interval is already the constant
PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS
so it should work with it. I've even tried with 16 minutes but does not work either.Another suggestion was using unique ID in case they are cancelling each other as described here. I've added a tag and started the worker as follows:
WorkManager.getInstance(this).enqueueUniquePeriodicWork( UNIQUE_ID, ExistingPeriodicWorkPolicy.REPLACE, myWorkRequest);
However this does not change and it still runs only once.
I read that WorkManager has some bugs and might not work well on some phones. But I tried with different devices and emulator, and it does not work in any of them.
My WorkManager version "androidx.work:work-runtime:2.5.0"
and currently I'm testing on an Android 11 emulator.
Any help would be appreciated, thanks!