Choosing WorkManager for Reliable Background Work on Android
Learn how Android WorkManager guarantees background work survives app restarts and reboots, with a concrete example of a charging‑aware log uploader and guidance on its limits.
23 Feb 2026, 16:37 UTC

Problem: Background work that survives app restarts
When you need to upload logs, sync data, or perform periodic maintenance, a simple background thread or Service can be killed by the system when the app goes to the background, the device low on memory, or after a reboot. This leads to missed work and a poor user experience.
Thesis: WorkManager is the preferred API for deferrable, guaranteed background work
WorkManager abstracts JobScheduler, AlarmManager and the legacy Firebase JobDispatcher, picking the best implementation based on API level, battery state, and network connectivity. It guarantees execution even if the app is killed or the device restarts, as long as the defined constraints are met.
Worked example: A charging‑aware log uploader
First add the WorkManager dependency in your app‑level build.gradle:
dependencies {
implementation "androidx.work:work-runtime:2.9.0"
}
Create a Worker that performs the upload. The worker runs on a background thread provided by WorkManager.
public class LogUploadWorker extends Worker {
public LogUploadWorker(@NonNull Context context, @NonNull WorkerParameters params) {
super(context, params);
}
@NonNull
@Override
public Result doWork() {
// Replace with your actual upload logic
boolean success = uploadLogsToServer();
return success ? Result.success() : Result.retry();
}
private boolean uploadLogsToServer() {
// Network call; requires INTERNET permission in manifest
// Return true on success, false otherwise
return false; // placeholder
}
}
Define a WorkRequest that only runs when the device is charging and on an unmetered network:
Constraints constraints = new Constraints.Builder()
.setRequiresCharging(true)
.setRequiredNetworkType(NetworkType.UNMETERED)
.build();
OneTimeWorkRequest uploadRequest = new OneTimeWorkRequest.Builder(LogUploadWorker.class)
.setConstraints(constraints)
.setBackoffCriteria(BackoffPolicy.EXPONENTIAL, Duration.ofSeconds(30))
.build();
WorkManager.getInstance(context).enqueue(uploadRequest);
Place the above code where you have a valid Context, for example in MainActivity.onCreate or after a user action. No special permission is required for WorkManager itself; however, the worker needs the INTERNET permission if it performs network calls.
To verify that the work is scheduled correctly:
- Run the app on a device or emulator (API 14+).
- Open Android Studio’s WorkManager Inspector (View → Tool Windows → WorkManager Inspection).
- You should see the
uploadRequestin the QUEUED state. - Plug the device into charger and ensure an unmetered network (Wi‑Fi). The work should transition to RUNNING and then either SUCCESS or RETRY based on your upload logic.
- Reboot the device; after it boots and meets the constraints, the work will be re‑queued automatically.
Trade‑offs and limitations
- Not for high‑frequency work: WorkManager imposes a minimum interval (typically 15 minutes) for periodic work; attempting to schedule work more often will be throttled.
- Chain complexity: Long chains of dependent
WorkRequestobjects become hard to debug; keep chains short or split into separate requests. - Long‑running tasks: If you need to keep the CPU awake for more than ~10 minutes, use a foreground service instead of a Worker, because WorkManager may stop the work when system resources are low.
You can check whether your use case fits by measuring the expected execution frequency and required runtime. If the task must run every few seconds or needs to keep the device awake indefinitely, consider alternatives.
Actionable closing
Start with a simple OneTimeWorkRequest that has a single constraint (e.g., requires charging). Use the WorkManager Inspector to confirm the request moves through QUEUED → RUNNING → SUCCESS. Write unit tests with TestWorker and TestWorkManager to validate retry logic without needing a device. Once the basic pattern works, add more constraints or chain additional workers as needed, always keeping the graph manageable.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.