Android 性能优化(11)网络优化( 7)Optimizing for Doze and App Standby
Optimizing for Doze and App Standby
In this document
Starting from Android 6.0 (API level 23), Android introduces two power-saving features that extend battery life for users by managing how apps behave when a device is not connected to a power source. Dozereduces battery consumption by deferring background CPU and network activity for apps when the device is unused for long periods of time. App Standby defers background network activity for apps with which the user has not recently interacted.
Doze and App Standby manage the behavior of all apps running on Android 6.0 or higher, regardless whether they are specifically targeting API level 23. To ensure the best experience for users, test your app in Doze and App Standby modes and make any necessary adjustments to your code. The sections below provide details.
Understanding Doze
If a user leaves a device unplugged and stationary for a period of time, with the screen off, the device enters Doze mode. In Doze mode, the system attempts to conserve battery by restricting apps' access to network and CPU-intensive services. It also prevents apps from accessing the network and defers their jobs, syncs, and standard alarms.
Periodically, the system exits Doze for a brief time to let apps complete their deferred activities. During thismaintenance window, the system runs all pending syncs, jobs, and alarms, and lets apps access the network.

Figure 1. Doze provides a recurring maintenance window for apps to use the network and handle pending activities.
At the conclusion of each maintenance window, the system again enters Doze, suspending network access and deferring jobs, syncs, and alarms. Over time, the system schedules maintenance windows less and less frequently, helping to reduce battery consumption in cases of longer-term inactivity when the device is not connected to a charger.
As soon as the user wakes the device by moving it, turning on the screen, or connecting a charger, the system exits Doze and all apps return to normal activity.
Doze restrictions
The following restrictions apply to your apps while in Doze:
- Network access is suspended.
- The system ignores wake locks.
- Standard
AlarmManager
alarms (includingsetExact()
andsetWindow()
) are deferred to the next maintenance window. - If you need to set alarms that fire while in Doze, use
setAndAllowWhileIdle()
orsetExactAndAllowWhileIdle()
. - Alarms set with
setAlarmClock()
continue to fire normally — the system exits Doze shortly before those alarms fire.
- If you need to set alarms that fire while in Doze, use
- The system does not perform Wi-Fi scans.
- The system does not allow sync adapters to run.
- The system does not allow
JobScheduler
to run.
Doze checklist
- If possible, use GCM fordownstream messaging.
- If your users must see a notification right away, make sure to use a GCM high priority message.
- Provide sufficient information within the initial message payload, so subsequent network access is unnecessary.
- Set critical alarms with
setAndAllowWhileIdle()
andsetExactAndAllowWhileIdle()
. - Test your app in Doze.
Adapting your app to Doze
Doze can affect apps differently, depending on the capabilities they offer and the services they use. Many apps function normally across Doze cycles without modification. In some cases, you must optimize the way that your app manages network, alarms, jobs, and syncs. Apps should be able to efficiently manage activities during each maintenance window.
Doze is particularly likely to affect activities thatAlarmManager
alarms and timers manage, because alarms in Android 5.1 (API level 22) or lower do not fire when the system is in Doze.
To help with scheduling alarms, Android 6.0 (API level 23) introduces two new AlarmManager
methods:setAndAllowWhileIdle()
and setExactAndAllowWhileIdle()
. With these methods, you can set alarms that will fire even if the device is in Doze.
Note: Neither setAndAllowWhileIdle()
nor setExactAndAllowWhileIdle()
can fire alarms more than once per 9 minutes, per app.
The Doze restriction on network access is also likely to affect your app, especially if the app relies on real-time messages such as tickles or notifications. If your app requires a persistent connection to the network to receive messages, you should use Google Cloud Messaging (GCM) if possible.
To confirm that your app behaves as expected with Doze, you can use adb commands to force the system to enter and exit Doze and observe your app’s behavior. For details, see Testing with Doze and App Standby.
Understanding App Standby
App Standby allows the system to determine that an app is idle when the user is not actively using it. The system makes this determination when the user does not touch the app for a certain period of time and none of the following conditions applies:
- The user explicitly launches the app.
- The app has a process currently in the foreground (either as an activity or foreground service, or in use by another activity or foreground service).
- The app generates a notification that users see on the lock screen or in the notification tray.
When the user plugs the device into a power supply, the system releases apps from the standby state, allowing them to freely access the network and to execute any pending jobs and syncs. If the device is idle for long periods of time, the system allows idle apps network access around once a day.
Using GCM to Interact with Your App While the Device is Idle
Google Cloud Messaging (GCM) is a cloud-to-device service that lets you support real-time downstream messaging between backend services and apps on Android devices. GCM provides a single, persistent connection to the cloud; all apps needing real-time messaging can share this connection. This shared connection significantly optimizes battery consumption by making it unnecessary for multiple apps to maintain their own, separate persistent connections, which can deplete the battery rapidly. For this reason, if your app requires messaging integration with a backend service, we strongly recommend that you use GCM if possible, rather than maintaining your own persistent network connection.
GCM is optimized to work with Doze and App Standby idle modes by means of high-priority GCM messages. GCM high-priority messages let you reliably wake your app to access the network, even if the user’s device is in Doze or the app is in App Standby mode. In Doze or App Standby mode, the system delivers the message and gives the app temporary access to network services and partial wakelocks, then returns the device or app to idle state.
High-priority GCM messages do not otherwise affect Doze mode, and they don’t affect the state of any other app. This means that your app can use them to communicate efficiently while minimizing battery impacts across the system and device.
As a general best practice, if your app requires downstream messaging, it should use GCM. If your server and client already uses GCM, make sure that your service uses high-priority messages for critical messages, since this will reliably wake apps even when the device is in Doze.
Support for Other Use Cases
Almost all apps should be able to support Doze by managing network connectivity, alarms, jobs, and syncs properly, and using GCM high-priority messages. For a narrow set of use cases, this might not be sufficient. For such cases, the system provides a configurable whitelist of apps that are partially exempt from Doze and App Standby optimizations.
An app that is whitelisted can use the network and hold partial wake locks during Doze and App Standby. However, other restrictions still apply to the whitelisted app, just as they do to other apps. For example, the whitelisted app’s jobs and syncs are deferred, and its regular AlarmManager
alarms do not fire. An app can check whether it is currently on the exemption whitelist by calling isIgnoringBatteryOptimizations()
.
Users can manually configure the whitelist in Settings > Battery > Battery Optimization. Alternatively, the system provides ways for apps to ask users to whitelist them.
- An app can fire the
ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS
intent to take the user directly to the Battery Optimization, where they can add the app. - An app holding the
REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
permission can trigger a system dialog to let the user add the app to the whitelist directly, without going to settings. The app fires aACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
Intent to trigger the dialog. - The user can manually remove apps from the whitelist as needed.
Before asking the user to add your app to the whitelist, make sure the app matches the acceptable use cases for whitelisting.
Note: Google Play policies prohibit apps from requesting direct exemption from Power Management features in Android 6.0+ (Doze and App Standby) unless the core function of the app is adversely affected.
Testing with Doze and App Standby
To ensure a great experience for your users, you should test your app fully in Doze and App Standby.
Testing your app with Doze
You can test Doze mode by following these steps:
- Configure a hardware device or virtual device with an Android 6.0 (API level 23) or higher system image.
- Connect the device to your development machine and install your app.
- Run your app and leave it active.
- Shut off the device screen. (The app remains active.)
- Force the system to cycle through Doze modes by running the following commands:
$ adb shell dumpsys battery unplug
$ adb shell dumpsys deviceidle stepYou may need to run the second command more than once. Repeat it until the device state changes to idle.
- Observe the behavior of your app after you reactivate the device. Make sure the app recovers gracefully when the device exits Doze.
Testing your app with App Standby
To test the App Standby mode with your app:
- Configure a hardware device or virtual device with an Android 6.0 (API level 23) or higher system image.
- Connect the device to your development machine and install your app.
- Run your app and leave it active.
- Force the app into App Standby mode by running the following commands:
$ adb shell dumpsys battery unplug
$ adb shell am set-inactive <packageName> true - Simulate waking your app using the following commands:
$ adb shell am set-inactive <packageName> false
$ adb shell am get-inactive <packageName> - Observe the behavior of your app after waking it. Make sure the app recovers gracefully from standby mode. In particular, you should check if your app's Notifications and background jobs continue to function as expected.
Acceptable Use Cases for Whitelisting
The table below highlights the acceptable use cases for requesting or being on the Battery Optimizations exceptions whitelist. In general, your app should not be on the whitelist unless Doze or App Standby break the core function of the app or there is a technical reason why your app cannot use GCM high-priority messages.
For more information, see Support for Other Use Cases.
Type | Use-case | Can use GCM? | Whitelisting acceptable? | Notes |
---|---|---|---|---|
Instant messaging, chat, or calling app. | Requires delivery of real-time messages to users while device is in Doze or app is in App Standby. | Yes, using GCM | Not Acceptable | Should use GCM high-priority messages to wake the app and access the network. |
Yes, but is not using GCM high-priority messages. | ||||
Instant messaging, chat, or calling app; enterprise VOIP apps. | No, can not use GCM because of technical dependency on another messaging service or Doze and App Standby break the core function of the app. | Acceptable | ||
Task automation app | App's core function is scheduling automated actions, such as for instant messaging, voice calling, new photo management, or location actions. | If applicable. | Acceptable | |
Peripheral device companion app | App's core function is maintaining a persistent connection with the peripheral device for the purpose of providing the peripheral device internet access. | If applicable. | Acceptable | |
App only needs to connect to a peripheral device periodically to sync, or only needs to connect to devices, such as wireless headphones, connected via standard Bluetooth profiles. | If applicable. | Not Acceptable |
Android 性能优化(11)网络优化( 7)Optimizing for Doze and App Standby的更多相关文章
- Android 性能优化(4)Optimizing Layout Hierarchies:用Hierarchy Viewer和Layoutopt优化布局
Optimizing Layout Hierarchies This lesson teaches you to Inspect Your Layout Revise Your Layout Use ...
- Android M 特性 Doze and App Standby模式详解
版权声明:本文由郑桂涛原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/185 来源:腾云阁 https://www.qclo ...
- Android性能优化典范(二)
Google前几天刚发布了Android性能优化典范第2季的课程,一共20个短视频,包括的内容大致有:电量优化,网络优化,Wear上如何做优化,使用对象池来提高效率,LRU Cache,Bitmap的 ...
- android app性能优化大汇总(google官方Android性能优化典范 - 第2季)
Google前几天刚发布了Android性能优化典范第2季的课程,一共20个短视频,包括的内容大致有:电量优化,网络优化,Wear上如何做优化,使用对象池来提高效率,LRU Cache,Bitmap的 ...
- Android性能优化典范 - 第2季
Google发布了Android性能优化典范第2季的课程,一共20个短视频,包括的内容大致有:电量优化,网络优化,Wear上如何做优化,使用对象池来提高效率,LRU Cache,Bitmap的缩放,缓 ...
- Android性能优化问题总结
性能优化这块,分为UI性能优化.内存优化.数据库优化.网络优化.耗电优化等等.可以从1.如何发现问题,2.怎么解决问题,3.解决效果对比,这几个方面去描述.举个简单例子——UI优化,可以从 UI出现什 ...
- Android性能优化之渲染
Google近期在Udacity上发布了Android性能优化的在线课程,目前有三个篇章,分别从渲染,运算与内存,电量三个方面介绍了如何去优化性能,这些课程是Google之前在Youtube上发布的A ...
- Android 性能优化探究
使用ViewStub动态载入布局.避免一些不常常的视图长期握住引用: ViewStub的一些特点: 1. ViewStub仅仅能Inflate一次,之后ViewStub对象被置空:某个被ViewStu ...
- 我把阿里、腾讯、字节跳动、美团等Android性能优化实战整合成了一个PDF文档
安卓开发大军浩浩荡荡,经过近十年的发展,Android技术优化日异月新,如今Android 11.0 已经发布,Android系统性能也已经非常流畅,可以在体验上完全媲美iOS. 但是,到了各大厂商手 ...
随机推荐
- Thinkphp5.0 的请求方式
Thinkphp5.0 的请求方式 方法一(使用框架提供的助手函数): public function index(){ $request = request(); dump($request); } ...
- spring boot 学习-创建方式
spring boot是什么 spring boot 是一个快速开发框架,适合小白快速上手开发,它集成了很多优秀的和常用的第三方框架,它简化了xml配置,完全采用注解方式,内部集成了Tomcat.Je ...
- JSP的表单处理
以下内容引用自http://wiki.jikexueyuan.com/project/jsp/form-processing.html: 当需要从浏览器向Web服务器传递一些信息并最终将信息返回到后端 ...
- Mybatis 最强大的动态sql <where>标签
<select id="findActiveBlogLike" resultType="Blog"> SELECT * FROM BLOG WHER ...
- [BestCoder Round #3] hdu 4907 Task schedule (模拟简单题)
Task schedule Problem Description 有一台机器,而且给你这台机器的工作表.工作表上有n个任务,机器在ti时间运行第i个任务,1秒就可以完毕1个任务. 有m个询问,每一个 ...
- SPOJ 15. The Shortest Path 最短路径题解
本题就是给出一组cities.然后以下会询问,两个cities之间的最短路径. 属于反复询问的问题,临时我仅仅想到使用Dijsktra+heap实现了. 由于本题反复查询次数也不多,故此假设保存全部最 ...
- 网页设计中11 款最好CSS框架
网页设计和发展领域已经成为竞争激烈的虚拟世界.想要在网络的虚拟世界中生存,仅有一堆静止的在线网络应用是远远不够的,网页必须要有很多功能,配以让人无法抗拒的设计.网页编码一定要合适.精确,才能保证不发生 ...
- 模式识别之ocr---文字识别Tesseract-OCR 进行文字识别 VS2010
近日做铸件文字识别的项目,需要识别铸件上的字符和数字,找到开源的识别库Tesseract,下面简单记录下怎么使用. 首先在项目主页http://code.google.com/p/tesseract- ...
- 图像处理之基础---yuv420及其rgb,bayer, yuv, RGB的相互转换详解
YUV格式解析1(播放器——project2) 根据板卡api设计实现yuv420格式的视频播放器 打开*.mp4;*.264类型的文件,实现其播放. 使用的视频格式是YUV420格式 YUV格式 ...
- Swift 1.1语言第7章 函数和闭包
Swift 1.1语言第7章 函数和闭包 在编程中,随着处理问题的越来越复杂.代码量飞速添加. 当中,大量的代码往往相互反复或者近似反复.假设不採有效方式加以解决.代码将非常难维护. 为了解决问题, ...