When should Android use WorkManager instead of a Foreground Service?
Question
Your Android app has two background jobs:
- Job A: Upload
40 MBof offline inspection photos once the phone has unmetered Wi-Fi and is not low on battery. It must survive app restarts and phone reboots. - Job B: Record a live 45-minute GPS trail while the user bikes with the screen locked.
A teammate runs both on raw threads inside a plain Service. Under Doze mode, Job A stalls and vanishes after a reboot. Job B dies two minutes after the screen locks. How do you redesign both jobs on Android 15?
Show hints (3)
- "WHY RAW SERVICES DIE"
Android restricts plain background services, batches network access under Doze mode, and wipes in-memory threads on process death or reboot.
- "DEFERRABLE VERSUS IMMEDIATE"
Job A can wait for Wi-Fi and must persist across reboots. Job B is immediate and visible to the user right now.
- "PICK THE RIGHT API"
Use
WorkManagerwithConstraints,ExistingWorkPolicy, and exponential backoff for Job A. Use alocationForeground Service with a notification for Job B.
Show checked explanation
Move Job A to WorkManager with NetworkType.UNMETERED and setRequiresBatteryNotLow(true), enqueued via enqueueUniqueWork (ExistingWorkPolicy.KEEP) and BackoffPolicy.EXPONENTIAL. Move Job B to a Foreground Service started from the foreground, showing an ongoing notification and declaring android:foregroundServiceType="location" with matching permissions.
WorkManager constraint gating, exponential backoff bounds (10 s to 5 h), and Android 15 foreground service rules were checked in a Python 3.12 model against Android 15 (API 35) and WorkManager 2.10 documentation. Checked by an editor other than the author.
Walkthrough
Android's Doze mode and App Standby defer network access, wake locks, and standard alarms once a device sits idle. Raw threads inside a plain Service have no persistence and get killed quickly after backgrounding.
| mechanism | survives Doze and reboot | notification | best fit |
|---|---|---|---|
Plain Service + thread | no | no | none for background work |
WorkManager | yes (SQLite + JobScheduler) | optional | deferrable, guaranteed work |
| Foreground Service | no reboot auto-resume | required | live, user-visible work |
Job A (deferrable and persistent): WorkManager stores work requests in its local SQLite database and hands them to JobScheduler. Android then runs the 40 MB upload during maintenance windows and resumes it after a reboot.
val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.UNMETERED)
.setRequiresBatteryNotLow(true)
.build()
val request = OneTimeWorkRequestBuilder<PhotoUploadWorker>()
.setConstraints(constraints)
.setBackoffCriteria(BackoffPolicy.EXPONENTIAL, 30, TimeUnit.SECONDS)
.build()
WorkManager.getInstance(context).enqueueUniqueWork(
"inspection_upload", ExistingWorkPolicy.KEEP, request
)ExistingWorkPolicy.KEEP prevents duplicate queues if the user triggers sync twice while an upload is active. When PhotoUploadWorker returns Result.retry(), backoff delays stay clamped between WorkRequest.MIN_BACKOFF_MILLIS (10 seconds) and WorkRequest.MAX_BACKOFF_MILLIS (5 hours), starting from 30 seconds by default.
Job B (live and user-visible): GPS recording cannot wait for Doze maintenance windows. Start a Foreground Service while the user is in the app, call startForeground() with an ongoing notification, and declare android:foregroundServiceType="location" plus FOREGROUND_SERVICE_LOCATION and runtime location permissions in AndroidManifest.xml.
Common mistakes
- Trying to launch a Foreground Service from the background on Android 12+, which throws
ForegroundServiceStartNotAllowedException. - Omitting
android:foregroundServiceTypeon Android 14+ throwsMissingForegroundServiceTypeException. MissingFOREGROUND_SERVICE_LOCATIONor runtime location permission throwsSecurityException. - Using
ExistingWorkPolicy.REPLACEblindly on an active upload, cancelling in-flight progress every time the screen reopens. - Configuring a retry delay below
10seconds (MIN_BACKOFF_MILLIS) or abusing adataSyncforeground service past Android 15's 6-hour limit.
Follow-up questions
- "Your Worker returns Result.retry() four times with 30-second exponential backoff. What are the minimum delays?" WorkManager doubles the delay on each retry:
30seconds,60seconds,120seconds, and240seconds, before Doze or constraint scheduling. Every delay stays clamped between10seconds (MIN_BACKOFF_MILLIS) and5hours (MAX_BACKOFF_MILLIS). - "What happens on Android 15 if a dataSync Foreground Service runs past 6 hours?" Android 15 limits
dataSyncforeground services to6hours in any 24-hour window. When that quota expires, Android callsService.onTimeout(). If the service does not callstopSelf()promptly, the system crashes the app. - "When would you choose a User-Initiated Data Transfer job over standard WorkManager?" Pick it when the user explicitly starts a large transfer, such as uploading a 2 GB video right now. It starts immediately without Doze deferral, and the system still handles network drops cleanly.
What a strong answer covers
Matches deferrable persistent uploads to WorkManager with constraints, unique policy, and backoff bounds, and matches live GPS tracking to a typed Foreground Service with an ongoing notification.
Open in interactive practice deck →