Skip to content

Instantly share code, notes, and snippets.

@codeniko
Forked from christophermark/Doze_Mode_Behavior.md
Created August 3, 2019 04:01
Show Gist options
  • Save codeniko/138d4d5af2e7c1e2fd224d64a9cabe6e to your computer and use it in GitHub Desktop.
Save codeniko/138d4d5af2e7c1e2fd224d64a9cabe6e to your computer and use it in GitHub Desktop.
Android Marshmallow Idle Mode Behavior

What is "idle mode"?

Idle mode is a state where the phone has no network connectivity and apps are temporarily suspended. Idle mode gets triggered by one of two ways: an asleep, still, unplugged phone (Doze mode), and when an app has not been opened in a significant amount of time (App Standby Mode). During idle mode via Doze, the phone will wake up periodically for a short amount of time, allowing apps to run and use network connectivity. Using several different methods outlined below, it is possible to wake apps up during idle mode outside of these maintenance windows.

Time to enter Doze mode

1 hour of being still after the screen turns off.

Time to enter App Standby mode

TBD

Idle maintenance windows

While the phone Dozes, it will wake up periodically to allow apps to do work and to update information with network connectivity. The longer the phone dozes, the longer the time between these maintenance windows.

Time between wakeups after entering idle mode:

  • 1 hour
  • 2 hours
  • 4 hours
  • 6 hours (continues at 6 hour intervals)

# App behavior during idle mode It seems as if any running processes are allowed to finish during idle mode. However, the OS does not provide network connectivity, ignores wakelocks, and doesn't allow the running of Alarms, JobScheduler jobs, and sync adapters.

Detecting network connectivity

Deceptively, trying to detect network connectivity through the ConnectivityManager is unreliable. It may tell you that the appp has network connectivity when it in fact does not.
Related: https://code.google.com/p/android-developer-preview/issues/detail?id=3164

Detecting when the app is Dozing

Detect when the app enters/exits idle mode due to Doze
The OS will send a DEVICE_IDLE_MODE_CHANGED broadcast so we can track when the app has entered or exited idle mode. Note: This broadcast will be sent when the device occasionally starts or ends the 10 minute idle maintenance window.
See: https://developer.android.com/reference/android/os/PowerManager.html#ACTION_DEVICE_IDLE_MODE_CHANGED)

Detect if the app is in idle mode
The isDeviceIdleMode() method in the PowerManager allows you to check if the device is currently in idle mode due to Doze.
See: https://developer.android.com/reference/android/os/PowerManager.html#isDeviceIdleMode()

Detecting when the app is in App Standby Mode

TBD
Note: apps in standby mode may return false from isDeviceIdleMode()


# Methods to wake your app from idle ## set[Exact]AndAllowWhileIdle() Wakes up the _app_, providing 10 seconds of network connectivity. Idle mode does not change and other applications are not alerted to this wakeup.
  • We can only call these a minimum of 15 minutes apart.
  • It seems as if that restriction is lifted for when we just enter idle mode via Doze.
  • Calling them closer than 15 minutes apart defaults to 15 minutes exactly.

setAlarmClock()

When I tried to wake the app using this method, my wakelock was indefinite - the phone didn't go back into idle mode. It's worth noting that wakeups via this method will allow other apps to do work and use network access, at the expense of the current app's battery stats. Additionally, Android will wake up the phone a small period before the scheduled alarm—presumably to allow other apps to perform syncing and maintenance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment