๐Ÿงช Testing, Debugging, and Performance

Battery Optimization for Android Apps

Users notice battery drain and uninstall the culprits. Android itself aggressively limits background activity to protect batteries. Battery-friendly apps work with those limits instead of fighting them.

โœ๏ธ Yunok Tech ๐Ÿ“… Updated โฑ 3 min read ๐ŸŽฏ Intermediate
On this page
  1. How Android protects battery
  2. Background work: let WorkManager decide when
  3. Wake locks: avoid them
  4. Location and sensors
  5. Network
  6. Foreground services
  7. Measuring battery impact
  8. Real examples
  9. Common mistakes
  10. Frequently asked questions
  11. Conclusion

The biggest battery consumers on a phone are the screen, the radios (cellular, Wi-Fi, GPS) and the CPU staying awake. Your app influences the last two directly: how often it wakes the device, uses the network, and requests location. Small design decisions add up to large differences.

How Android protects battery

Android power management mechanisms Doze defers network access, jobs and alarms when the device is idle, allowing periodic maintenance windows. App Standby Buckets limit background work based on how recently and often the app is used. Background execution limits restrict background services. Battery-related vitals report excessive wake locks and background activity. Dozeidle device: defersnetwork, jobs, alarmsmaintenance windows Standby Bucketsactive โ†’ rareless used apps getfewer resources Background limitsno free-runningbackground services Android vitalswake lock andbackground usagemetrics
Design background work so it fits these mechanisms, and let the system batch it efficiently.

Background work: let WorkManager decide when

Battery-aware periodic syncKotlin
val sync = PeriodicWorkRequestBuilder<SyncWorker>(12, TimeUnit.HOURS)
    .setConstraints(
        Constraints(
            requiredNetworkType = NetworkType.UNMETERED,   // Wi-Fi, avoids mobile data
            requiresBatteryNotLow = true,
            requiresCharging = false,
        )
    )
    .build()

WorkManager.getInstance(context).enqueueUniquePeriodicWork(
    "content-sync", ExistingPeriodicWorkPolicy.KEEP, sync,
)

WorkManager respects Doze and standby, batches work with other apps, and retries with backoff. Choose the longest period your feature tolerates. See Background Work with WorkManager.

Wake locks: avoid them

A partial wake lock keeps the CPU running with the screen off. Android vitals flags excessive wake lock usage. Most apps never need one directly: WorkManager holds wake locks for you while workers run. If you must use one, always set a timeout and release it in finally.

Location and sensors

Balanced, lifecycle-bound location updatesKotlin
val request = LocationRequest.Builder(Priority.PRIORITY_BALANCED_POWER_ACCURACY, 60_000L)
    .setMinUpdateIntervalMillis(30_000L)
    .setMinUpdateDistanceMeters(50f)          // skip updates for tiny movements
    .build()
// Start in onStart / LifecycleStartEffect, remove updates in onStop.
  • Use the lowest accuracy your feature needs (city weather doesn't need GPS precision).
  • Prefer the last known location or a single current-location request over continuous updates.
  • Stop updates when the UI isn't visible, unless the user explicitly started a tracking session (then use a foreground service).

Network

  • Batch and prefetch: fewer, larger requests let the radio sleep between them.
  • Cache aggressively. See Caching Strategies.
  • Replace polling with push (FCM) for updates.
  • Keep WebSockets open only while the relevant screen is visible.

Foreground services

Foreground services are for ongoing, user-visible tasks (navigation, playback, workout tracking), and they require a notification and a declared type. Don't use them to keep the app alive for convenience. See Foreground Services.

Measuring battery impact

  • Android Studio Power Profiler (on supported devices) shows estimated power use by component.
  • Background Task Inspector shows WorkManager jobs, alarms and wake locks.
  • Battery Historian / bugreports for detailed investigation.
  • Android vitals for field data on wake locks and background usage.

Real examples

News app

Replaced 15-minute polling with FCM-triggered refresh plus a 12-hour unmetered sync, which greatly reduced background network use.

Fitness tracker

High-accuracy location only during a user-started workout in a foreground service, and none otherwise.

Chat app

A socket only while a conversation is open, and FCM for everything else.

Common mistakes

  • Using AlarmManager exact alarms for routine sync.
  • Continuous GPS for features that need coarse location occasionally.
  • Retrying failed network calls in tight loops.
  • Asking users to disable battery optimisation for the app without a strong, policy-compliant reason.

Frequently asked questions

Why does my background work run late?

Doze, standby buckets and manufacturer power management defer work. For work that must happen at an exact time, such as alarms the user set, use the appropriate exact-alarm APIs and permissions. Otherwise accept deferral.

Do manufacturers add extra restrictions?

Some OEMs apply additional battery management. Test on popular devices among your users and design for deferred work.

Does dark mode save battery?

On OLED screens, darker pixels use less power, so supporting dark theme can help users save battery.

Conclusion

Battery-friendly apps defer and batch background work with WorkManager, avoid manual wake locks, request only the location accuracy and frequency they need, push instead of poll, and measure with the Power Profiler and Android vitals.

Next step: learn the tools in depth with Android Studio Profiler: A Practical Guide.

Official references

๐Ÿ“ฑ Apps built by Yunok Tech

Real Android apps on Google Play, free to download.

See all our apps โ†’

Ready to build real Android applications?

Performance work starts with understanding how Android apps are built and how they behave at runtime. Strengthen those fundamentals and the profiling tools start to make sense.

Move beyond theory and strengthen your Android development skills with Yunoktech's Android course. Learn core concepts, practical implementation, modern Android development techniques, and the reasoning behind building maintainable applications.

Explore the Android course and take your next step as an Android developer.

Share: LinkedIn X / Twitter WhatsApp