背景掃描 Wi-Fi 次數過多
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
當應用程式在背景掃描 Wi-Fi 時會喚醒 CPU,從而造成電池耗電。如果掃描次數過多,裝置的電池壽命可能會明顯有降低情形。如果應用程式處於 PROCESS_STATE_BACKGROUND
或 PROCESS_STATE_CACHED
狀態,系統就視為在背景執行。
本文將說明如何偵測應用程式在背景掃描 Wi-Fi 太多次,並提供如何診斷及解決問題的訣竅。
偵測問題
您可能永遠都不會知道應用程式有掃描 Wi-Fi 次數異常的問題。如果您已經發布應用程式,Android Vitals 可以讓您注意並進而修正這個問題。
Android Vitals
Android Vitals 會透過 Play 管理中心在應用程式於背景掃描太多次 Wi-Fi 時發出提醒,藉此改善應用程式的效能。如果應用程式每小時掃描超過 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,那麼採用「Lazy First」策略可能會有幫助。Lazy First 涵蓋了三種技術,您可以用來減少掃描 Wi-Fi 次數:「減少」、「延後」和「合併」。這些技術的相關資訊請參見「針對電池壽命進行最佳化」。
為您推薦
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[null,null,["上次更新時間: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)"]]