android WorkManager AlarmManager Android Kotlin Background Tasks Jetpack Beginner

WorkManager vs AlarmManager in Android — Which One Should You Use?

Stop guessing — here is when to use each one

WorkManager vs AlarmManager in Android — Which One Should You Use?

If you've ever needed to run a task in the background — like syncing data, sending a notification, or downloading a file — you've probably run into two options: WorkManager and AlarmManager. Both schedule tasks, but they work very differently. Picking the wrong one can drain your user's battery, break on newer Android versions, or simply not work at all.

This guide explains both clearly so you can make the right choice every time.

---

The Simple Answer First

💡 Use WorkManager for almost everything. Use AlarmManager only when you need tasks to fire at an exact time, like an alarm clock or a calendar reminder.

That's the rule. Now let's understand why.

---

What is WorkManager?

WorkManager is a Jetpack library that handles background work reliably. It is the modern, recommended way to schedule tasks that need to run even if the app is closed or the device restarts.

WorkManager handles all the complexity for you:

Works on all Android versions (API 14+)

Survives app restarts and device reboots

Respects battery optimization automatically

Supports chaining tasks one after another

Retries failed tasks automatically

💡 Google officially recommends WorkManager for all deferrable background work. If your task does not need to run at an exact second, use WorkManager.

---

What is AlarmManager?

AlarmManager is an older Android system service that lets you schedule tasks at a precise time. Think of it like setting a phone alarm — it fires at exactly the time you set, even if the device is in low-power mode.

But there are serious trade-offs:

Does not survive device reboots unless you handle it manually

Android 12+ requires special permission for exact alarms

Can be killed by battery optimization if not handled carefully

More complex to implement correctly

⚠️ Since Android 12 (API 31), apps must request SCHEDULE_EXACT_ALARM permission to use exact alarms. Users can revoke this permission at any time. Always handle the case where permission is denied.

---

Side by Side Comparison

Guaranteed execution — WorkManager ✅ / AlarmManager ⚠️ (can be killed)

Survives reboot — WorkManager ✅ / AlarmManager ❌ (needs extra setup)

Battery friendly — WorkManager ✅ / AlarmManager ❌

Exact timing — WorkManager ❌ / AlarmManager ✅

Recommended by Google — WorkManager ✅ / AlarmManager only for alarms

Complexity — WorkManager Low / AlarmManager High

---

When to Use WorkManager

Use WorkManager for any task that:

Needs to run in the background even after the app closes

Does not need to run at an exact millisecond

Should retry if it fails

Needs to run only when conditions are met (like Wi-Fi connected)

Real examples: syncing data to a server, compressing images, uploading files, sending analytics, refreshing content.

Basic WorkManager Example

First add the dependency:

groovybuild.gradle
implementation("androidx.work:work-runtime-ktx:2.9.0")

Create a Worker class:

kotlinSyncWorker.kt
import androidx.work.Worker
import androidx.work.WorkerParameters
import android.content.Context

class SyncWorker(context: Context, params: WorkerParameters) : Worker(context, params) {

    override fun doWork(): Result {
        // your background task here
        syncDataToServer()
        return Result.success()
    }

    private fun syncDataToServer() {
        // your sync logic
    }
}

Schedule it from your Activity or ViewModel:

kotlin
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager

val syncRequest = OneTimeWorkRequestBuilder<SyncWorker>().build()
WorkManager.getInstance(context).enqueue(syncRequest)
💡 For tasks that repeat, use PeriodicWorkRequestBuilder instead of OneTimeWorkRequestBuilder. Minimum repeat interval is 15 minutes.

---

When to Use AlarmManager

Use AlarmManager only when:

You need a task to fire at a precise exact time

You are building an alarm clock app

You are building a calendar reminder

Timing accuracy matters more than battery efficiency

⚠️ AlarmManager is NOT for general background work. If you just need to sync data periodically, use WorkManager instead.

Basic AlarmManager Example

kotlin
import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent

fun scheduleExactAlarm(context: Context, triggerTimeMillis: Long) {
    val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager

    val intent = Intent(context, AlarmReceiver::class.java)
    val pendingIntent = PendingIntent.getBroadcast(
        context, 0, intent,
        PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
    )

    alarmManager.setExactAndAllowWhileIdle(
        AlarmManager.RTC_WAKEUP,
        triggerTimeMillis,
        pendingIntent
    )
}
🚨 On Android 12+, you must check canScheduleExactAlarms() before calling setExactAndAllowWhileIdle(). If the user revoked the permission, your app will crash.

---

Common Mistakes Beginners Make

1. Using AlarmManager for syncing data

Very common mistake. If you just want to sync every few hours, WorkManager is the right tool. AlarmManager is overkill and battery-unfriendly for this.

2. Not handling reboot with AlarmManager

⚠️ AlarmManager tasks are cleared when the device reboots. You must register a BroadcastReceiver for BOOT_COMPLETED and reschedule your alarms manually. WorkManager handles this automatically.

3. Setting repeat interval below 15 minutes with WorkManager

WorkManager enforces a minimum repeat interval of 15 minutes for periodic work. If you need something more frequent, reconsider your architecture.

---

Quick Decision Guide

Need to sync data in background? → WorkManager

Need to upload a file when Wi-Fi connects? → WorkManager

Need to build an alarm clock? → AlarmManager

Need a calendar notification at exact time? → AlarmManager

Not sure? → WorkManager

---

Summary

WorkManager — reliable, battery friendly, survives reboots, recommended for most tasks

AlarmManager — precise timing, complex setup, use only for exact-time tasks like alarms

Android 12+ requires permission for exact alarms — always check before scheduling

When in doubt, WorkManager is almost always the right choice

Background task scheduling in Android has improved a lot over the years. WorkManager takes care of the hard parts so you can focus on what your app actually does.

Asif Rahman
Asif Rahman

Indie Product Engineer focused on toolcraft — building free tools that just work.

← Back to Blog Try Free Tools ⚡