'Recomendation for daily background work in Android
I just want to know any of your experiences scheduling daily jobs that perform tasks like data upload with POST API request and then update the local database with room. My goal is to perform this kind of task daily arround 23:59 hours.
I´ve tried One Time unique work and then reschedule it in the same worker after finish the task, this was working pretty good on some devices but it failed in other ones.
I also tried a Periodic Unique Work, but this is pretty unstable it only works propertly if the device is plugged to power source if not it delays the work many times.
What do you recomend me to use for this kind of work the most acurate posible the goal is to perform the work when betwen 23:59 and 06:00 to avoid to interfere in the user experience during the day.
Solution 1:[1]
Alarm manager + Broadcast receiver + service: Here you need to make the service a foreground service with a notification. Your service will always run even if the app is terminated or not running. For restart you can have a reboot receiver and set the alarm again. With this approach the service can run wherein a lot of time is needed say downloading of multiple files as it can run until the job finishes completely.
WorkManager: With WorkManager the main problem is that the behavior is not consistent across all mobile manufacturers. Mobile manufactures like Samsung have restrictions for battery optimization and the WorkManager would not work when the app is not running or terminated. The WorkManager is not good in situation where in a lot of time is needed say downloading of multiple files as it would get terminated after some minutes (10 minutes if am not wrong)
Work Manager not working when app is killed by the user. Why?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Govinda Paliwal |