Advanced AOSP Subsystems
3 min read

Doze Mode

Introduction to Doze Mode

Introduced in Android 6.0 (Marshmallow), Doze mode is a systemic power-saving feature designed to extend battery life when a device is left unattended. Instead of allowing apps to wake the device arbitrarily, Doze mode restricts background network access, defers background jobs, and ignores standard wakelocks.

Doze Mode Levels

Doze operates in two distinct phases to balance power savings with timely notifications.

Light Doze

Light Doze engages shortly after the screen is turned off, regardless of whether the device is moving.

  • Restrictions: Network access is suspended for most apps. Periodic syncing and background jobs (JobScheduler) are deferred.
  • Allowed: Wakelocks are still respected, and high-priority FCM (Firebase Cloud Messaging) messages can reach the app.
  • Trigger: Screen off for a short period.

Deep Doze

Deep Doze is the aggressive power-saving state.

  • Restrictions: Network access is severed. Wakelocks are entirely ignored. AlarmManager alarms (including exact alarms) are deferred. GPS and Wi-Fi scanning are disabled.
  • Trigger: Screen off, on battery power, and the device has been completely stationary for a longer period (detected via the significant motion sensor).

The Doze State Machine

The DeviceIdleController service manages the transition through various Doze states.

  1. ACTIVE: Device is awake and in use.
  2. INACTIVE: Screen turned off. Timer starts for Light Doze.
  3. IDLE_PENDING: Transitioning to Deep Doze. Waiting for motion sensor confirmation.
  4. IDLE: The device is in Deep Doze. Restrictions are fully applied.
  5. IDLE_MAINTENANCE: The device temporarily lifts restrictions to process deferred jobs.

Maintenance Windows

To prevent apps from breaking completely, Doze periodically opens short "maintenance windows" (the IDLE_MAINTENANCE state).

During a maintenance window:

  • Network access is restored.
  • Deferred jobs and syncs are executed.
  • Pending alarms are fired.

As the device remains stationary longer, these maintenance windows occur less frequently, maximizing power savings.

# Force the device into Doze mode for testing
adb shell dumpsys deviceidle force-idle

# Un-force the device from Doze mode
adb shell dumpsys deviceidle unforce

Whitelisting Apps from Doze

Some applications, such as task automation tools or VPN clients, require continuous background execution. Users can exempt these apps from Doze restrictions.

The Exemption Process

  1. Settings UI: Users can navigate to Battery Optimization settings and mark an app as "Not Optimized."
  2. Intent Request: Apps can prompt the user to whitelist them using the ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS intent.
// Check if the app is currently ignoring battery optimizations
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
boolean isIgnoring = pm.isIgnoringBatteryOptimizations(getPackageName());

if (!isIgnoring) {
    Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
    intent.setData(Uri.parse("package:" + getPackageName()));
    startActivity(intent);
}

Impact of Whitelisting

When an app is whitelisted:

  • It retains network access during Doze.
  • It can acquire and hold partial wakelocks.
  • Note: It is still subject to App Standby bucket restrictions and other background execution limits. Whitelisting should be used sparingly, as it significantly degrades battery life.