Navigating the Maze: Long-Running Background Work in Android and Its Quirks
In the last blog we saw how User initiated Data transfer is yet another API introduced from Android 14.
What’s surprising (or maybe not, given Android’s reputation 😅) is that deciding which API to use for long-running background work is a maze of conditions, version checks, and restrictions.
Let’s break this down with a simple requirement:
The Problem
A user captures large amounts of data, which needs to sync whenever an internet connection is available.
- It doesn’t need to be instant.
- It just needs to be reliable when network conditions allow.
If you’re an experienced Android developer, what’s your first thought?
WorkManager? Persistent, reliable, background work — it seems like a perfect fit.
I am sure you have seen this diagram somewhere —
If Only It Were That Simple…
So let’s go step by step through this ever-evolving Android landscape:
1. WorkManager Should Be Enough, Right?
Since our work needs to be persistent and reliable, WorkManager seems like the way to go.
But wait… our task is long-running.
WorkManager alone isn’t enough — we need a Foreground Service to keep the job alive.
2. Okay, Let’s Add a Foreground Service!
We modify our Worker
to call setForegroundAsync()
/ setForeground()
, showing a notification.
Problem solved?
If only!
3. Foreground Services Need Special Handling Below Android 12
Foreground services inside Worker
s don’t work below Android 12.
📌 Solution: Add a version check before setting the Worker
as a foreground service.
Problem solved?
If only!
4. Android 14 Requires Foreground Service Type Declarations
Android 14 enforces Foreground Service Types.
📌 Solution: Declare dataSync
as a foreground service type in the manifest.
📍 Reference
Problem solved?
If only!
5. Android 15 Introduces a 6-Hour Limit on Foreground Services
Starting in Android 15, all of an app’s foreground services share a 6-hour time limit.
📌 Solution: We can use User-Initiated Data Transfer (UIDT) Jobs to bypass this restriction (introduced in Android 14+).
Problem solved?
If only!
UIDTs Have No Backward Compatibility
UIDTs only work on Android 14+.
For Android 13 and below, we still have to rely on Foreground Services + WorkManager.
Now, finally, we’re done. (Until next release )
So, here’s my own version of the classic background work decision diagram to summarize these conditions!
If you have any questions or comments, please fee free to ask. Let’s discuss this further.
Reference links -