1 page.title=Power Management 2 @jd:body 3 4 <!-- 5 Copyright 2015 The Android Open Source Project 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 --> 19 <div id="qv-wrapper"> 20 <div id="qv"> 21 <h2>In this document</h2> 22 <ol id="auto-toc"></ol> 23 </div> 24 </div> 25 26 <p>Battery life is a perennial user concern. To extend battery life, Android 27 continually adds new features and optimizations to help the platform optimize 28 the off-charger behavior of applications and devices.</p> 29 <p>Android includes the following battery life enhancements:</p> 30 31 <ul> 32 <li><strong><a href="#app-standby">App Standby</strong></a>. The platform can 33 place unused applications in App Standby mode, temporarily restricting network 34 access and deferring syncs and jobs for those applications.</li> 35 <li><strong><a href="#doze">Doze</strong></a>. The platform can enter a state of 36 deep sleep (periodically resuming normal operations) if users have not actively 37 used their device (screen off and stationary) for extended periods of time. 38 Android 7.0 also enables Doze to trigger a lighter set of optimizations when 39 users turn off the device screen yet continue to move around.</li> 40 <li><strong><a href="#exempt-apps">Exemptions</strong></a>. System apps and 41 cloud messaging services preloaded on a device are typically exempted from App 42 Standby and Doze by default (although app developers can intent their 43 applications into this setting). Users can exempt applications via the Settings 44 menu.</li> 45 </ul> 46 47 <p>The following sections describe these enhancements.</p> 48 49 <h2 id="app-standby">App Standby</h2> 50 <p>App Standby extends battery life by deferring background network activity 51 and jobs for applications the user is not actively using.</p> 52 53 <h3 id="app-standby-life">App Standby lifecycle</h3> 54 <p>The platform detects inactive applications and places them in App Standby 55 until the user begins actively engaging with the application.</p> 56 57 <table> 58 <tbody> 59 <tr> 60 <th width=46%>Detection</th> 61 <th width=23%>During App Standby</th> 62 <th width=31%>Exit</th> 63 </tr> 64 65 <tr> 66 <td><p>The platform detects an application is inactive when the device is not 67 charging <strong>and</strong> the user has not launched the application directly 68 or indirectly for a specific amount of clock time as well as a specific amount 69 of screen-on time. (Indirect launches occur when a foreground app accesses a 70 service in a second app.)</p></td> 71 <td><p>The platform prevents applications from accessing the network more than 72 once a day, deferring application syncs and other jobs.</p></td> 73 <td><p>The platform exits the app from App Standby when:</p> 74 <ul> 75 <li>Application becomes active.</li> 76 <li>Device is plugged in and charging.</li> 77 </ul> 78 </td> 79 </tr> 80 </tbody> 81 </table> 82 83 <p>Active applications are unaffected by App Standby. An application is active 84 when it has:</p> 85 <ul> 86 <li>A process currently in the foreground (either as an activity or foreground 87 service, or in use by another activity or foreground service), such as 88 notification listener, accessibility services, live wallpaper, etc.</li> 89 <li>A notification viewed by the user, such as in the lock screen or 90 notification tray.</li> 91 <li>Explicitly been launched by the user.</li> 92 </ul> 93 <p>An application is inactive if none of the above activities has occurred for 94 a period of time. 95 </p> 96 97 <h3 id=testing_app_standby>Testing App Standby</h3> 98 <p>You can manually test App Standby using the following ADB commands:</p> 99 100 <pre> 101 $ adb shell dumpsys battery unplug 102 $ adb shell am set-idle packageName true 103 $ adb shell am set-idle packageName false 104 $ adb shell am get-idle packageName 105 </pre> 106 107 <h2 id="doze">Doze</h2> 108 109 <p>Doze extends battery life by deferring application background CPU and 110 network activity when a device is unused for long periods.</p> 111 112 <p>Idle devices in Doze periodically enter a maintenance window, during which 113 apps can complete pending activities (syncs, jobs, etc.). Doze then resumes sleep 114 for a longer period of time, followed by another maintenance window. The 115 platform continues the Doze sleep/maintenance sequence, increasing the length of 116 idle each time, until a maximum of a few hours of sleep time is reached. At all 117 times, a device in Doze remains aware of motion and immediately leaves Doze 118 if motion is detected.</p> 119 120 <p>Android 7.0 extends Doze to trigger a lighter set of optimizations every time 121 a user turns off the device screen, even when the user continues to move around, 122 enabling longer lasting battery life.</p> 123 124 <p>System services (such as telephony) may be preloaded and exempted from Doze 125 by default. Users can also exempt specific applications from Doze in the 126 Settings menu. By default, Doze is <strong>disabled</strong> in the Android Open 127 Source Project (AOSP). For details on enabling Doze, see 128 <a href="#integrate-doze">Integrating Doze</a>.</p> 129 130 <h3 id="doze-reqs">Doze requirements</h3> 131 <p>Doze support requires the device has a cloud messaging service, such as 132 <a href="https://developers.google.com/cloud-messaging/">Google Cloud Messaging 133 (GCM)</a>. This enables the device to know when to wake from Doze.</p> 134 <p>Full Doze support also requires a 135 <a href="{@docRoot}devices/sensors/sensor-types.html#significant_motion">Significant 136 Motion Detector (SMD)</a> on the device; however, the lightweight Doze mode in 137 Android 7.0 does not require an SMD. If Doze is enabled on a device that:</p> 138 <ul> 139 <li>Has an SMD, full Doze optimizations occur (includes lightweight 140 optimizations).</li> 141 <li>Does not have an SMD, only the lightweight Doze optimizations occur.</li> 142 </ul> 143 144 145 <h3 id="doze-life">Doze lifecycle</h3> 146 147 <p>Doze begins when the platform detects the device is idle and 148 ends when one or more exit criteria activities occur.</p> 149 150 <table> 151 <tbody> 152 <tr> 153 <th width=20%>Detection</td> 154 <th width=60%>During Doze</th> 155 <th width=20%>Exit</th> 156 </tr> 157 <tr> 158 <td><p>The platform detects a device is idle when:</p> 159 <ul> 160 <li>Device is stationary (using significant motion detector).</li> 161 <li>Device screen is off for some amount of time.</li> 162 </ul> 163 <p>Doze mode does not engage when the device is plugged into a power charger. 164 </p> 165 </td> 166 <td><p>The platform attempts to keep the system in a sleep state, periodically 167 resuming normal operations during a maintenance window then returning the device 168 to sleep for longer repeating periods. During sleep, the following 169 restrictions are active:</p> 170 <ul> 171 <li>Apps not allowed network access.</li> 172 <li>App wakelocks ignored.</li> 173 <li>Alarms deferred. Excludes alarm clock alarms and alarms set using 174 <code>setAndAllowWhileIdle()</code> (limited to 1 per 15 minutes per app while 175 in Doze). This exemption is intended for apps (such as Calendar) that must show 176 event reminder notifications.</li> 177 <li>Wi-Fi scans not performed.</li> 178 <li>SyncAdapter syncs and JobScheduler jobs deferred until the next maintenance 179 window.</li> 180 <li>Apps receiving SMS and MMS messages are put on a temporary whitelist so 181 they can complete their processing.</li> 182 </ul> 183 </td> 184 <td><p>The platform exits the device from Doze when it detects:</p> 185 <ul> 186 <li>User interaction with device.</li> 187 <li>Device movement.</li> 188 <li>Device screen turns on.</li> 189 <li>Imminent AlarmClock alarm.</li> 190 </ul> 191 <p>Notifications do not cause the device to exit from Doze.</p> 192 </td> 193 </tr> 194 </tbody> 195 </table> 196 197 <p>Android 7.0 extends Doze by enabling a lightweight sleep mode during screen 198 off, before the device is idle.</p> 199 <p><img src="../images/doze_lightweight.png"></p> 200 <p class="img-caption">Figure 1. Doze modes for non-stationary and stationary 201 devices.</p> 202 203 <table> 204 <tbody> 205 <tr> 206 <th>Action</th> 207 <th>Doze</th> 208 <th>Lightweight Doze</th> 209 </tr> 210 <tr> 211 <td>Trigger</td> 212 <td>Screen off, on battery, stationary</td> 213 <td>Screen off, on battery (unplugged)</td> 214 </tr> 215 <tr> 216 <td>Timing</td> 217 <td>Successively increasing periods with maintenance</td> 218 <td>Repeated N-minute periods with maintenance windows</td> 219 </tr> 220 <tr> 221 <td>Restrictions</td> 222 <td>No network access, wake lock, or GPS/Wi-FI scan. Alarms and jobs/syncs deferred.</td> 223 <td>No network access. Jobs/syncs deferred except during maintenance windows.</td> 224 </tr> 225 <tr> 226 <td>Behavior</td> 227 <td>Only high-priority push notification messages received.</td> 228 <td>All real-time messages (instant messages, calls, etc.) received. High-priority push notification message enables temporary network access.</td> 229 </tr> 230 <tr> 231 <td>Exit</td> 232 <td>Motion, screen on, or alarm clock alarm.</td> 233 <td>Screen on.</td> 234 </tr> 235 </tbody> 236 </table> 237 238 <h3 id="doze-interactions">Interaction with App Standby</h3> 239 <ul> 240 <li>Time spent in Doze does not count towards App Standby.</li> 241 <li>While the device is in Doze, idle applications are allowed to perform normal 242 operations at least once a day.</li> 243 </ul> 244 245 <h3 id="integrate-doze">Integrating Doze</h3> 246 247 <p>When Doze is enabled, devices that support 248 <a href="{@docRoot}devices/sensors/sensor-types.html#significant_motion">SENSOR_TYPE_SIGNIFICANT_MOTION</a> 249 will perform full Doze optimizations (includes lightweight optimizations); 250 devices without an SMD will perform only lightweight Doze optimizations. Android 251 automatically selects the appropriate Doze optimizations and no vendor 252 configuration is necessary.</p> 253 254 <p>To enable Doze for a device, perform the following tasks:</p> 255 256 <ol> 257 <li>Confirm the device has a cloud messaging service installed.</li> 258 <li>In the device overlay config file 259 <code>overlay/frameworks/base/core/res/res/values/config.xml</code>, set 260 <code>config_enableAutoPowerModes</code> to <strong>true</strong>: 261 <pre> 262 bool name="config_enableAutoPowerModes">true</bool> 263 </pre> 264 <br>In AOSP, this parameter is set to false (Doze disabled) by default.<br> 265 </li> 266 <li>Confirm that preloaded apps and services: 267 <ul> 268 <li>Use the 269 <a href="https://developer.android.com/training/monitoring-device-state/doze-standby.html">power-saving 270 optimization guidelines</a>. For details, see <a href="#test-apps">Testing and 271 optimizing applications</a>. 272 <p><strong>OR</strong></p> 273 <li>Are exempted from Doze and App Standby. For details, see 274 <a href="#exempt-apps">Exempting applications</a>.</li> 275 </ul> 276 </li> 277 <li>Confirm the necessary services are exempted from Doze.</a> 278 </li> 279 </ol> 280 281 <h4 id="doze-tips">Tips</h4> 282 <ul> 283 <li>If possible, use GCM for 284 <a href="https://developers.google.com/cloud-messaging/downstream">downstream 285 messaging</a>.</li> 286 <li>If your users must see a notification right away, use a 287 <a href="https://developers.google.com/cloud-messaging/concept-options#setting-the-priority-of-a-message">GCM high priority 288 message</a>.</li> 289 <li>Provide sufficient information within the initial 290 <a href="https://developers.google.com/cloud-messaging/concept-options#payload">message 291 payload</a> (to avoid unnecessary subsequent network access).</li> 292 <li>Set critical alarms with 293 <a href="http://developer.android.com/reference/android/app/AlarmManager.html#setAndAllowWhileIdle(int,%20long,%20android.app.PendingIntent)">setAndAllowWhileIdle()</a> 294 and 295 <a href="http://developer.android.com/reference/android/app/AlarmManager.html#setExactAndAllowWhileIdle(int,%20long,%20android.app.PendingIntent)">setExactAndAllowWhileIdle()</a>. 296 </li> 297 </ul> 298 299 <h4 id="test-apps">Testing and optimizing applications</h4> 300 <p>Test all applications (especially preloaded applications) in Doze mode. For 301 details, refer to 302 <a href="https://developer.android.com/training/monitoring-device-state/doze-standby.html#testing_doze_and_app_standby">Testing 303 Doze and App Standby</a>.</p> 304 305 <p class="note"><strong>Note</strong>: MMS/SMS/Telephony services function independently 306 of Doze and will always wake client apps even while the device remains in Doze 307 mode.</p> 308 309 <h2 id="exempt-apps">Exempting applications</h2> 310 <p>You can exempt applications from being subject to Doze or App Standby. 311 Exemptions may be needed in the following use cases:</p> 312 <ul> 313 <li>OEM using non-GCM Cloud Messaging platform</li> 314 <li>Carrier using non-GCM Cloud Messaging platform</li> 315 <li>Third-party application using non-GCM Cloud Messaging platform</li> 316 </ul> 317 318 <p class="warning"><strong>Warning</strong>: Do not exempt apps to avoid testing 319 and optimizing. Unnecessary exemptions undermine the benefits of Doze and App 320 Standby and can compromise the user experience, so we strongly suggest 321 minimizing such exemptions as they allow applications to defeat beneficial 322 controls the platform has over power use. If users become unhappy about the 323 power consumption of these apps, it can lead to frustration, bad experiences 324 (and negative user reviews for the app), and customer support questions. For 325 these reasons, we strongly recommend that you do not exempt third-party 326 applications and instead exempt only cloud messaging services or apps with 327 similar functions.</p> 328 329 <p>Apps exempted by default are listed in a single view within the Settings > 330 Battery menu. This list is used for exempting the app from both Doze and App 331 Standby modes. To provide transparency to the user, the Settings menu 332 <strong>MUST</strong> show all exempted applications.</p> 333 334 <p>Users can manually exempt apps via Settings > Battery > Battery optimization 335 > All apps and then selecting the app to turn off (or back on) optimization. 336 However, users cannot unexempt any application or service that is exempted by 337 default in the system image.</p> 338