download

When should Android use WorkManager instead of a Foreground Service?

Mobile~10 minReviewed 2 Dec 2026Free · No sign-in

Question

Your Android app has two background jobs:

  • Job A: Upload 40 MB of 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)
  1. "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.

  2. "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.

  3. "PICK THE RIGHT API"

    Use WorkManager with Constraints, ExistingWorkPolicy, and exponential backoff for Job A. Use a location Foreground 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.

mechanismsurvives Doze and rebootnotificationbest fit
Plain Service + threadnononone for background work
WorkManageryes (SQLite + JobScheduler)optionaldeferrable, guaranteed work
Foreground Serviceno reboot auto-resumerequiredlive, 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.

"KOTLIN"
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:foregroundServiceType on Android 14+ throws MissingForegroundServiceTypeException. Missing FOREGROUND_SERVICE_LOCATION or runtime location permission throws SecurityException.
  • Using ExistingWorkPolicy.REPLACE blindly on an active upload, cancelling in-flight progress every time the screen reopens.
  • Configuring a retry delay below 10 seconds (MIN_BACKOFF_MILLIS) or abusing a dataSync foreground 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: 30 seconds, 60 seconds, 120 seconds, and 240 seconds, before Doze or constraint scheduling. Every delay stays clamped between 10 seconds (MIN_BACKOFF_MILLIS) and 5 hours (MAX_BACKOFF_MILLIS).
  • "What happens on Android 15 if a dataSync Foreground Service runs past 6 hours?" Android 15 limits dataSync foreground services to 6 hours in any 24-hour window. When that quota expires, Android calls Service.onTimeout(). If the service does not call stopSelf() 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 →

Original OfferVibe question. Android background work · Doze and App Standby · WorkRequest reference · Foreground services · Foreground service timeout.
© 2026 OfferVibe · Text CC BY-NC-SA 4.0 · code MITFree to share and adapt with credit, not for commercial use. Code samples are free for any use.