I'm working on an android app,
I need to run a task periodically every 12 hours, i don't mind if its not exact, i just need the task to indeed run(I don't care even if it will run once per 15 hours, I just need to make sure it runs about every 12-24 hours)
My task is not very long, takes at most a few seconds.
From what I'v understood, WorkManager is the way to go in this case(short deferrable tasks that have to run periodically)
I have the following problem - The task seem to run the first time immediately(this is what i want), but it does not run again even after a day or more although(even if i use the app!) i'v set the delay between tasks to be 12 hours
I'm using periodic work request, and my code looks something like this:
val MyRequest =
PeriodicWorkRequestBuilder<MyWork>(12, TimeUnit.HOURS)
.setInitialDelay(0, TimeUnit.MILLISECONDS)
.build()
WorkManager.getInstance(context)
.enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.KEEP, MyRequest)
this code is currently called from the main activity on create
even if i close the app and open it again after 30 hours, still the work does not run, it only runs one time, if i uninstall the app and install again, it only runs once. when i debug i can see that the code i added runs everytime when "on create" runs, but the work does not run. If i use ExistingPeriodicWorkPolicy.REPLACE, then it works everytime, but i want the work to run once per 12 hours, and not everytime when "on create" is running, which is why i used ExistingPeriodicWorkPolicy.KEEP
- note that i did not set constraints, workmanager states in its documentation that the default is no constraints at all
Any idea what could be causing this or how to fix this? Was i wrong choosing WorkManager?
I saw this post , maybe this explains my problem?(I'm using a Samsung device)
But even if this explains the problem, it does not explain why the task is not running for a second time if i open the app after 2 days, but it does work 1 time when installing and opening it?
Thanks!!