Skip to content

Instantly share code, notes, and snippets.

@christophermark
Last active August 20, 2021 21:37
Show Gist options
  • Save christophermark/430d4ddf3a46a02672c1 to your computer and use it in GitHub Desktop.
Save christophermark/430d4ddf3a46a02672c1 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.

@camhart
Copy link

camhart commented Dec 22, 2017

Thanks for this. How are you listening to DEVICE_IDLE_MODE_CHANGED when exiting doze mode? I'm finding it's not being triggered for me.

Edit: Nevermind--It's only triggered when the app is running. Once you close the app its no longer triggered.

@Xgamefactory
Copy link

Xgamefactory commented Mar 13, 2018

isDeviceIdleMode returns false and this is very bad. did not find any solution yet.
you can ignore doze mode prompting user to ignore battery optimization

just call intent action REQUEST_IGNORE_BATTERY_OPTIMIZATIONS with data "package:"+ you application id

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