自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
流量节省程序模式
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
移动数据流量费用高昂,在大众普遍无法承担数据服务费用的地方更是如此。因此,Android 用户迫切希望能够降低应用的数据使用量,或完全阻止应用使用数据流量。Android 7.0 版本中的流量节省程序为用户提供了这项功能。
流量节省程序功能可由用户开启或关闭。应用开发者应使用新的 API 来检查流量节省程序模式是否已开启。如果已开启,则应用开发者可以调整其应用以降低或杜绝流量消耗,从而妥善应对这种情况。
这项功能为最终用户带来极大的好处,因为他们将能够决定哪些应用可以在后台使用流量,而哪些应用只能在前台使用流量。这样就可以确保在流量节省程序处于开启状态时(根据用户设置),实现所需的后台数据流量限制。
实现
由于流量节省程序是平台中的一项功能,因此在默认情况下,设备制造商可在 N 版本中获得相关功能。请在以下位置查找源文件:
packages/apps/Settings/src/com/android/settings/datausage
设置界面
Android 开源项目 (AOSP) 中提供了默认的“流量节省程序设置”界面。有关示例,请查看以下屏幕截图。
这些屏幕截图显示了使用中的流量节省程序模式。
图 1. 关闭/开启流量节省程序
图 2. 省电模式和流量节省程序同时开启
图 3. 特定应用流量使用情况屏幕:“Settings”>“Apps”>“Data usage”
图 4. “快捷设置”菜单中的流量节省模式状态
应用
重要提示:设备实现人员不得将应用列入白名单。即使他们这样做,用户也可以移除这类应用。如果在设备中加入了其他应用,可能会迫使用户决定针对这些应用使用流量节省程序。
所有应用开发者(包括在设备上预加载应用的 OEM 和运营商)都必须采取相应措施来实现流量节省程序。请参阅 developer.android.com 上的流量节省程序,了解有关检测和监控流量节省程序状态的应用开发者说明。合作伙伴可参阅下文以获得更多实用信息。
要针对流量节省程序模式进行优化,应用需要满足以下几点:
- 移除不必要的图片
- 为剩余图片使用低分辨率
- 使用低比特率视频
- 触发现有的“精简”体验
- 压缩数据
- 留意按流量计费/不按流量计费网络的状态(即使流量节省程序已关闭)。
另外,要良好地配合流量节省程序,应用不得:
- 自动播放视频
- 预提取内容/附件
- 下载更新/代码
- 要求列入白名单(除非实现核心功能需要在后台使用流量)
- 将列入白名单视为使用更多流量的许可
验证
实现人员可以运行以下 CTS 测试,从而确保其功能版本能够按预期运行。
com.android.cts.net.HostsideRestrictBackgroundNetworkTests
此外,还可以先运行以下命令来查看所有可用选项,从而使用 adb
命令手动执行测试:
adb shell cmd netpolicy
例如,以下命令将返回已列入白名单的应用的 UID:
adb shell cmd netpolicy list restrict-background-whitelist
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-03-11。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-03-11。"],[],[],null,["# Data Saver mode\n\nMobile data use is costly and even more so where data plan costs are not\naffordable by all. Android users need the ability to reduce data use or block it\nfrom apps altogether. The Data Saver feature in the Android 7.0 release provides\nthis functionality to the user.\n\n\nThe [Data Saver](https://developer.android.com/training/basics/network-ops/data-saver)\nfeature can be turned on or off by the user. App developers\nshould use a new API to check if Data Saver mode is on. If it is on, the app\ndevelopers can handle the situation gracefully by tuning their applications for\nlow- or no-data access.\n\n\nEnd users benefit as they will be able to control which apps can access data in\nthe background and which can access data only while in the foreground. This\nensures desired background data exchange when Data Saver is on per user control.\n\nImplementation\n--------------\n\n\nSince the Data Saver is a feature in the platform, device manufacturers gain its\nfunctionality by default with the N release. Find the source files in: \n\n[packages/apps/Settings/src/com/android/settings/datausage](https://android.googlesource.com/platform/packages/apps/Settings/+/android16-release/src/com/android/settings/datausage)\n\n### Settings interface\n\n\nA default Data Saver settings user interface is supplied in the Android Open\nSource Project (AOSP). See the screenshots below for examples.\n\n\nThese screenshots show the Data Saver mode in use.\n\n\n**Figure 1.** Toggling Data Saver off/on\n\n\n**Figure 2.** When both battery saver and Data Saver are on\n\n\n**Figure 3.** App-specific data usage screen: Settings \\\u003e Apps \\\u003e Data usage\n\n\n**Figure 4.** Data saver states on the Quick Settings menu\n\n### Apps\n\n**Important**: Device implementers should not whitelist apps.\nEven if they do, users may remove them. Including other apps forces users to decide on which to\napply Data Saver.\n\n\nAll app developers must act to implement Data Saver, including OEMs and\ncarriers with preloaded apps. See\n[Data\nSaver on developer.android.com](https://developer.android.com/training/basics/network-ops/data-saver) for app developer instructions on detecting\nand monitoring Data Saver states. See the sections below for additional details\nhelpful to partners.\n\n\nTo optimize for Data Saver mode, apps should:\n\n- Remove unnecessary images\n- Use lower resolution for remaining images\n- Use lower bitrate video\n- Trigger existing \"lite\" experiences\n- Compress data\n- Respect metered vs. unmetered network status even when Data Saver is off\n\n\nConversely, to work well with Data Saver, apps should not:\n\n- Autoplay videos\n- Prefetch content/attachments\n- Download updates / code\n- Ask to be whitelisted unless background data is truly part of core functionality\n- Treat whitelisting as a license to use more bandwidth\n\nValidation\n----------\n\n\nImplementers can ensure their version of the feature works as intended by\nrunning the following CTS test: \n\n```\ncom.android.cts.net.HostsideRestrictBackgroundNetworkTests\n```\n\n\nIn addition, `adb` commands can be used to conduct tests manually by\nfirst running this command to see all available options: \n\n```\nadb shell cmd netpolicy\n```\n\n\nFor example, this command returns the UIDs of the whitelisted apps: \n\n```\nadb shell cmd netpolicy list restrict-background-whitelist\n```"]]