Doze, WorkManager backoff, and background sync that stalls
androiddozeworkmanagerbackgroundsyncbattery
Periodic background work on Android is a request, not a promise. A phone sitting idle on a desk spends most of its time in Doze, network access is deferred, and WorkManager's retry backoff can push the next run out by hours. Design for "it will run eventually" and give the user a manual trigger.
Case: hourly sync silently stopped for 12 days
The guardian app's hourly SyncWorker stopped checking in while the messaging app (same phone, same server) kept syncing. Diagnosis:
- During Doze the worker hit "Unable to resolve host", returned Result.retry(), and exponential backoff grew until the job's minimum latency was about 5 hours; on an idle phone each attempt failed again.
- adb shell dumpsys jobscheduler showed last runs 13 days old with the TIMING_DELAY constraint unsatisfied.
- A manual "Sync now" worked instantly.
Mitigation: put both apps on the Doze whitelist over adb, which persists across reboots (and survives a later lockdown that disables adb):
adb shell dumpsys deviceidle whitelist +com.phone.guardian
adb shell dumpsys deviceidle whitelist +com.message.app
Other options: cap the backoff (linear policy, short initial delay), return success() on network errors for periodic work so the next period runs on schedule, and re-enqueue or sync when the app is opened.
Rules of thumb
- Periodic work: minimum interval 15 minutes; actual timing is decided by the OS.
- For "must happen without the user" (a parent's removals reaching a kid's phone), pair periodic work with boot and app-open triggers.
- Keep the last known good state on failure and never treat a failed fetch as "empty list" (return null for failure, empty for a real empty result).
- On OEM skins (ZTE MyOS and similar) aggressive killers can stop the process outright; the levers are battery-optimization exemption and the OEM autostart setting. Confirm with
ApplicationExitInfo(see Android app cold-start performance and process death).
Checking connectivity on a test phone
Do not use ping to a server that blocks ICMP; use adb shell dumpsys connectivity and look for VALIDATED.
See also
- 1:1 WebRTC voice calls over polled signaling and TURN (a visible high-priority push rings a killed app)