后台 Wi-Fi 扫描次数过多
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
当应用在后台执行 Wi-Fi 扫描时,会唤醒 CPU,从而加快耗电速度。扫描次数过多时,设备的电池续航时间可能会明显缩短。如果某个应用处于 PROCESS_STATE_BACKGROUND
或 PROCESS_STATE_CACHED
状态,则会被视为在后台运行。
本文档介绍了如何检测您的应用正在后台执行过多 Wi-Fi 扫描,并提供关于诊断和解决问题的提示。
检测问题
您有时可能并不知道自己的应用出现了过多 Wi-Fi 扫描的行为。如果您已经发布了应用,Android Vitals 可以告知您该问题,这样您便可以解决相应问题。
Android Vitals
当您的应用在后台执行过多 Wi-Fi 扫描时,Android Vitals 可通过 Play 管理中心提醒您,从而帮助您改善应用性能。如果应用每小时执行的扫描超过 4 次,Android Vitals 就认定 Wi-Fi 扫描次数过多。在 Play 管理中心内,您可以查看出现此行为的电池工作时段所占的百分比。
电池工作时段的定义取决于平台版本。
- 在 Android 10 中,电池工作时段是在给定的 24 小时内接收到的所有电池报告的汇总。电池报告是指两次电池充电(从低于 20% 充到 80% 以上或者从任意电量值充满到 100%)间隔的时间。
- 在 Android 11 中,电池工作时段是固定的 24 小时时间段。
如需了解 Google Play 如何收集 Android Vitals 数据,请参阅 Play 管理中心文档。
调查 Wi-Fi 扫描次数
Battery Historian 等工具可帮助您深入了解应用的扫描行为。Battery Historian 会针对每个应用直观呈现 Wi-Fi 扫描行为,可帮助您更清晰地了解应用的动态。如需详细了解 Battery Historian,请参阅使用 Battery Historian 分析耗电情况。
如需了解 Battery Historian 的使用机制,请参阅 Batterystats 和 Battery Historian 演示。
减少扫描次数
如果可能,应该将您的应用设置为在前台运行时执行 Wi-Fi 扫描。前台服务会自动显示通知;因此,当应用在前台执行 Wi-Fi 扫描时,用户会知道设备上发生 Wi-Fi 扫描的原因和时间。
如需了解如何让应用在前台运行时执行扫描,请参阅介绍 WifiManager
类的文档。
如果您的应用无法避免在后台运行期间执行 Wi-Fi 扫描,则可能适合采用偷懒至上策略。“偷懒至上”包含三种可用于消减 Wi-Fi 扫描次数的方法:“减少”“推迟”和“合并”。如需了解这些方法,请参阅为延长电池续航时间进行优化。
为您推荐
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Excessive Wi-Fi Scanning in the Background\n\nWhen an app performs Wi-Fi scans in the background, it wakes up the CPU,\ncausing rate of battery drain. When too many scans occur, the device's\nbattery life may be noticeably shortened. An app is considered to be running in\nthe background if it is in the `PROCESS_STATE_BACKGROUND` or\n`PROCESS_STATE_CACHED` state.\n\nThis document explains how to detect when your app is performing too many Wi-Fi\nscans in the background, and provides tips about diagnosing and addressing\nthe problem.\n\nDetect the problem\n------------------\n\nYou may not always know that your app is exhibiting an inordinate\nnumber of Wi-Fi scans. If you have already published your app,\nAndroid vitals can make you aware of the problem so that you can fix it.\n\n### Android vitals\n\nAndroid vitals can help improve your app's performance by alerting you via the\n[Play Console](https://play.google.com/console/) when your app is\nperforming excessive Wi-Fi scanning in the background. Android vitals considers\nWi-Fi scanning excessive when an app performs more than 4 scans per hour.\nIn Play Console you can check percentage of *battery sessions* that exhibit\nthis behavior.\n\nThe definition of *battery session* depends on the platform version.\n\n- In Android 10, a battery session is the aggregation of all battery reports received within a given 24-hour period. A *battery report* refers to the interval between two battery charges either from below 20% to above 80% or from any charge level to 100%.\n- In Android 11, a battery session is a fixed 24-hour period.\n\nFor information on how Google Play collects Android vitals data, see the [Play\nConsole](https://support.google.com/googleplay/android-developer/answer/7385505)\ndocumentation.\n\nInvestigate the Wi-Fi scans\n---------------------------\n\nTools such as Battery Historian can help you gain more insight into your\napp's scanning behavior. Battery Historian provides a visualization of Wi-Fi\nscanning behavior on a per-app basis, which can help you gain a clearer\npicture of what's happening with your app. For more information about Battery\nHistorian, see\n[Analyzing Power Use with Battery Historian](/topic/performance/power/battery-historian#asd).\n\nFor information about the mechanics of using Battery Historian, see\n[Batterystats and Battery Historian Walkthrough](/topic/performance/power/setup-battery-historian).\n\nReduce the scans\n----------------\n\nIf possible, your app should be performing Wi-Fi scans while the app is running\nin the foreground. Foreground services automatically present notifications;\nperforming Wi-Fi scans in the foreground thus makes the user aware of the\nwhy and when Wi-Fi scans take place on their device.\n\nFor information on how to scan while in the foreground,\nsee the documentation for the\n[`WifiManager`](/reference/android/net/wifi/WifiManager#startScan()) class.\n\nIf your app cannot avoid performing Wi-Fi scans while the app is running in\nthe background, it may benefit from applying\na [Lazy First](/topic/performance/power#lazy) strategy. Lazy First\nencompasses three techniques that you can use to cut down on Wi-Fi scans:\n*reduce* , *defer* , and *coalesce* .\nFor information about these techniques, see\n[Optimizing for Battery Life](/topic/performance/power).\n\nRecommended for you\n-------------------\n\n- Note: link text is displayed when JavaScript is off\n- [Excessive Mobile Network Usage in Background](/topic/performance/vitals/bg-network-usage)"]]