Prevent Your Android App From Going to Sleep on Oreo (API 26+)

Modern Android versions—starting with Android 8.0 Oreo—introduced strict battery-saving policies. These policies help users get more screen time, but they can also cause problems for developers who need their apps to keep running in the background.

If your app performs long-running tasks—such as tracking sensors, recording data, streaming audio, processing network requests, or maintaining continuous connectivity—you’ve probably noticed that Android can pause or delay your background work to conserve battery.

This guide explains why this happens, what changed in Android Oreo, and how you can prevent your app from going to sleep safely and correctly using WakeLocks.


Why Android Puts Apps to Sleep

Android aggressively manages background behavior to preserve battery. Depending on the scenario, the system may:

  • Delay tasks when the device is idle
  • Restrict background services
  • Lower CPU priority
  • Stop network access
  • Kill apps that abuse wakeups

This makes sense from a user perspective, but it can break apps that depend on continuous processing.


Android Oreo: Background Execution Limits

Starting from API 26, Android introduced several limitations:

1. Background Service Restrictions

Apps cannot freely run background services unless they use:

  • Foreground services (with notification)
  • JobScheduler / WorkManager

2. Doze Mode Enhancements

When the device is idle:

  • CPU is throttled
  • Network access becomes limited
  • Timers and alarms are deferred

3. App Standby Buckets

Apps that the user interacts with less often get stricter limits.

Because of these rules, developers sometimes need to explicitly keep the CPU awake.


What Is a WakeLock?

A WakeLock is a mechanism provided by Android’s PowerManager that allows an app to request that the device stay awake under certain conditions.

Types include:

  • PARTIAL_WAKE_LOCK — keeps CPU running
  • SCREEN_DIM_WAKE_LOCK — keeps screen on (deprecated)
  • FULL_WAKE_LOCK — keeps screen bright (deprecated)

For most modern apps, PARTIAL_WAKE_LOCK is the correct and only reliable choice.


How to Prevent Your App From Sleeping (WakeLock Example)

Here is a clean and safe implementation:

import android.content.Context;
import android.os.PowerManager;

public class WakeLockHelper {

    private PowerManager.WakeLock wakeLock;

    public void acquire(Context context) {
        if (wakeLock == null) {
            PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            if (pm != null) {
                wakeLock = pm.newWakeLock(
                        PowerManager.PARTIAL_WAKE_LOCK,
                        "MyApp:WakeLock"
                );
                wakeLock.acquire();
            }
        }
    }

    public void release() {
        if (wakeLock != null && wakeLock.isHeld()) {
            wakeLock.release();
            wakeLock = null;
        }
    }
}

How to use it

WakeLockHelper helper = new WakeLockHelper();

// When your background task starts
helper.acquire(context);

// When finished
helper.release();

Best Practices (Avoid Battery Drain!)

Android allows WakeLocks, but misusing them can cause overheating and heavy battery usage. Follow these rules:

Always release the WakeLock

Use try–finally blocks or lifecycle callbacks.

Use Foreground Services for visible background work

Starting with Oreo, this is the recommended pattern.

Use WorkManager for scheduled/background tasks

Google expects developers to use system-managed schedulers wherever possible.

Avoid holding WakeLocks longer than needed

Your app will be penalized if it drains too much battery.

Test on a real device in Doze Mode

adb shell dumpsys deviceidle force-idle
…is your friend.


When You SHOULD Use WakeLocks

Use WakeLocks only if your app truly requires uninterrupted CPU time, such as:

  • Location tracking
  • Fitness trackers
  • Data recorders
  • Bluetooth communication apps
  • IoT control systems
  • Audio/streaming services

If your app simply performs occasional work, a WakeLock may be unnecessary.


Conclusion

Preventing your app from going to sleep on Android Oreo and newer versions requires understanding the OS’s strict battery optimization rules. By using a PARTIAL_WAKE_LOCK alongside proper lifecycle management and system-approved background patterns (Foreground Services, WorkManager), you can keep your app running reliably without harming battery life.

Following these guidelines ensures your application remains stable, efficient, and user-friendly—even under Android’s tighter power policies.

This article is inspired by real-world challenges we tackle in our projects. If you're looking for expert solutions or need a team to bring your idea to life,

Let's talk!

    Please fill your details, and we will contact you back

      Please fill your details, and we will contact you back