Scheduling Deferrable Background Work on Android with WorkManager
Use WorkManager for uploads and syncs that can wait for network or charging, not for exact-time alarms or immediate UI tasks.
16 Jul 2025, 00:38 UTC

The upload that outlives your app
You need to send a crash log or user report to your server, but the user can close the app before the upload finishes. An exact delivery time does not matter; what matters is that the upload eventually happens when the device has network access. Using a foreground service for this is overkill and keeps a persistent notification visible. Using AlarmManager for exact timing is the wrong tool because the work is deferrable, not time-critical.
Takeaway: For work that can wait until constraints such as network availability or charging are met, use Android WorkManager. It persists work across process death and device restarts, but it does not promise an exact clock time.
What WorkManager actually guarantees
WorkManager is an Android Jetpack library for deferrable, constraint-based background work. A Worker is the unit of work; it runs on a background thread by default and must return Result.success(), Result.failure(), or Result.retry(). WorkManager stores requests in an internal database, so a reboot or app exit does not erase them.
The system decides when to run the work. Constraints such as NetworkType.CONNECTED or requiresBatteryNotLow() must be satisfied before execution. On Android 8.0 and higher, background execution restrictions and battery optimization buckets can delay work further if the system considers your app restricted.
Example: queue a network-dependent upload
Add the WorkManager runtime artifact to your module's build.gradle.kts. Use a version placeholder and resolve it from your project's version catalog or a recent stable 2.x release.
dependencies {
val workManagerVersion = "2.9.0" // verify against the latest stable 2.x release
implementation("androidx.work:work-runtime-ktx:$workManagerVersion")
}
Define a Kotlin worker that writes a marker to internal storage so you can verify execution later. Avoid holding UI references or blocking the main thread inside doWork().
class LogUploadWorker(
private val appContext: Context,
workerParams: WorkerParameters
) : CoroutineWorker(appContext, workerParams) {
override suspend fun doWork(): Result {
val logFile = File(appContext.filesDir, "pending_upload.log")
if (!logFile.exists()) {
return Result.failure()
}
// Network call would go here. Return based on the real outcome.
return Result.success()
}
}
Build a one-time request with a network constraint and enqueue it from your activity, fragment, or application class.
val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
val uploadRequest = OneTimeWorkRequestBuilder<LogUploadWorker>()
.setConstraints(constraints)
.setInitialDelay(15, TimeUnit.MINUTES)
.build()
WorkManager.getInstance(context).enqueue(uploadRequest)
Run this code in your app's process with normal user permissions; no root or special manifest permission is required beyond the standard internet permission if the worker actually uploads data. The system chooses execution timing, so the 15-minute delay is a lower bound, not a guarantee.
Diagnose with adb
To confirm whether the system has accepted the job, run from a terminal on your development machine:
adb shell dumpsys jobscheduler | grep -i your.package.name
You need USB debugging enabled on the device and the Android platform-tools on your host. Look for a job ID associated with your package and work request tag. If no job appears, the request may not have been enqueued or the system may have deferred it because constraints are unmet. Output format changes between Android releases, so treat line positions as unstable.
From doWork(), write a timestamp to internal storage or log to Logcat. After enabling network and waiting, check the file or logcat filter for your worker tag. Do not rely on emulator-only behavior; Doze and App Standby on physical devices can delay execution.
Limits: when WorkManager is the wrong choice
- Do not use it for exact-time alarms or user-facing real-time tasks. WorkManager is not a replacement for AlarmManager or exact alarms.
- Long-running workers can be terminated by the system. Keep
doWork()short and break large jobs into chunks if needed. - It is not suitable for immediate work that must finish while the user is watching the screen; use a coroutine or foreground service with appropriate justification instead.
- Testing requires the WorkManager test artifact and
TestDriver; device behavior can differ from emulators.
Checklist before you ship
- Confirm the work is deferrable and constraint-driven, not time-critical.
- Add
work-runtime-ktxand target a stable WorkManager 2.x release. - Set constraints that match your actual requirements, such as network or charging.
- Verify execution by writing a marker from
doWork()and checking it after the constraint is satisfied. - Test on a physical device with battery optimization enabled, not only on an emulator.
WorkManager is the right default for background uploads and syncs that can survive app closure. Match the constraint to the real-world condition your upload needs, verify on a physical device, and avoid assuming exact execution times.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.