Use WorkManager for Deferrable Android Background Work
Schedule deferrable Android tasks that survive process death and reboots with Jetpack WorkManager, including constraints, Gradle setup, and verification steps.
21 Feb 2026, 04:04 UTC

The problem: your upload dies when the screen turns off
You enqueue a log upload in an AsyncTask or a plain Thread, and it works while the user watches. Then the user locks the device, the system kills your process to reclaim memory, and the upload never finishes. If you switch to an alarm or a long-running service, Doze and App Standby can delay or block it entirely. The useful takeaway is to use Jetpack WorkManager for deferrable, constraint-based background work that must survive process death and reboots.
How WorkManager fits background work
WorkManager is an AndroidX library that schedules deferrable work—tasks that can run later, even if your app process is killed or the device restarts. On API 23 and higher it primarily uses JobScheduler, the system service that batches work and respects power-saving modes. On API 14 through 22 it falls back to an AlarmManager-based implementation with a BroadcastReceiver. You declare constraints such as required network type, battery-not-low, charging, or storage availability, and the scheduler runs the worker only when those conditions are met.
Worked example: upload logs when the network is available
Add the runtime dependency to your module's Gradle file. Run this in Android Studio or from the project root with the Gradle wrapper. Verify the latest stable version before committing; 2.9.0 is used here as an example.
dependencies {
implementation("androidx.work:work-runtime:2.9.0")
}
Create a worker class. Replace com.example.UploadWorker with your own package and class name.
package com.example
import android.content.Context
import androidx.work.ListenableWorker
import androidx.work.Worker
import androidx.work.WorkerParameters
class UploadWorker(context: Context, params: WorkerParameters) : Worker(context, params) {
override fun doWork(): Result {
// Perform your upload here. Return Result.success() on completion,
// Result.failure() if the upload should not be retried, or
// Result.retry() if a transient error occurred and WorkManager should
// reschedule according to its backoff policy.
return Result.success()
}
}
Enqueue the work from an activity, fragment, or application class. You need no special permission for WorkManager itself, but if your worker accesses the network, declare INTERNET in your manifest.
val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.setRequiresBatteryNotLow(true)
.build()
val uploadRequest = OneTimeWorkRequestBuilder()
.setConstraints(constraints)
.build()
val workId = WorkManager.getInstance(context).enqueue(uploadRequest)
Observe the result. Replace workId with the ID returned from enqueue.
WorkManager.getInstance(context)
.getWorkInfoByIdLiveData(workId)
.observe(lifecycleOwner) { workInfo ->
workInfo?.let {
when (it.state) {
WorkInfo.State.ENQUEUED -> { /* waiting for constraints */ }
WorkInfo.State.RUNNING -> { /* upload in progress */ }
WorkInfo.State.SUCCEEDED -> { /* upload finished */ }
WorkInfo.State.FAILED -> { /* upload did not complete */ }
else -> { /* still transitioning */ }
}
}
}
Expected checks: after enqueue, the state should move to ENQUEUED. When the device meets the network and battery constraints, it should transition to RUNNING and then SUCCEEDED or FAILED. A risk is that the work may not run immediately if the device is in Doze, App Standby, or Battery Saver, even when your declared constraints appear satisfied.
Limitations and when not to use it
- Periodic work repeats no more frequently than once every 15 minutes. If you need sub-15-minute execution, use a foreground service or an exact alarm with the special
SCHEDULE_EXACT_ALARMpermission. - Some device manufacturers apply aggressive background restrictions. WorkManager may not run unless the user whitelists your app in battery settings.
- In multi-process apps, initialize WorkManager through
Configuration.Provider; default process-only initialization can miss work in other processes.
Practical verification
- Force Doze and Battery Saver on a physical device or emulator.
- Trigger the work request and watch logcat for WorkManager scheduling and execution tags.
- Reboot the device with a persistent work request enqueued and confirm the worker runs after constraints are met post-reboot.
Rollback: if you decide WorkManager is not appropriate, remove the work-runtime dependency line, sync the project, and delete any worker classes and enqueue calls you added.
Bottom line
Use WorkManager when the work is deferrable, must survive process death, and can tolerate constraint-based scheduling. Use alarms or foreground services only when you need exact timing or immediate, user-visible execution.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.