tl;dr
How can I implement a background task to run every 1 minute, even in low-power or doze-mode, in Android API 35+, for periods longer than 6 hours?
Details
Requirements
- [R1] – A background service must run every 1-minute to call a web-server and update the database. If certain conditions are met, a notification must be shown to the user
- [R2] – The periodic check must run until the user stops it (or certain conditions are met). This may be for over 6 hours.
- [R3] – The periodic check must run even if the app is in doze mode
Implementation before API 34
The user with an action in the UI starts a Foreground Service
with a notification. The Foreground Service
starts a background task using Executor
which will perform the necessary actions and then sleep for 1 minute.
Implications with API 35
Since API 34, the Foreground Service
needs to have a Foreground Service Type
. The most relevant seem to be data_sync
. However, since API 35, this will be limited to 6-hours max running period, and then it will time out. This does not meet [R2] above. It also appears that data_sync
type will be deprecated in future releases.
Alternatives Examined
WorkManager
API
As it is documented here,
Periodic work has a minimum interval of 15 minutes
This will not meet [R1]
AlarmManager
API
When using the AlarmManager
, when the device is in low-power mode, the alarms cannot be triggered more frequently than 15-mins. This is also documented [here](https://developer.android.com/reference/android/app/AlarmManager#setAndAllowWhileIdle(int,%20long,%20android.app.PendingIntent)
Under normal system operation, it will not dispatch these alarms more than about every minute (at which point every such pending alarm is dispatched); when in low-power idle modes this duration may be significantly longer, such as 15 minutes.
This again fails requirement [R1]
What is the recommended way to achieve the above in Android API 35+ ?