部分喚醒鎖定停滯
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
部分 Wake Lock 是 PowerManager
API 中的一項機制,可以讓開發人員在關閉裝置螢幕 (無論是因為系統逾時還是因為使用者按下電源鍵) 之後繼續運行 CPU。應用程式可以使用 PARTIAL_WAKE_LOCK
標記呼叫 acquire()
,或使用其他可取得 Wake Lock 的 API,獲取部分 Wake Lock。當應用程式在背景執行時 (使用者看不到應用程式的任何部分),如果部分 Wake Lock 保持了較長時間,就會進入「停滯」狀態。這種狀況會阻止裝置進入電量較低的狀態,因此會快速消耗裝置電量。您應只在必要時使用部分 Wake Lock,並於不再需要時立即釋放。
如果應用程式進入部分 Wake Lock 停滯狀態,您可以根據本頁面的指南診斷並修正問題。
偵測問題
您不一定都會發現應用程式有部分 Wake Lock 進入停滯狀態。如果您已發布應用程式,Android Vitals 可協助您注意這個問題。
Android Vitals
Android Vitals 會透過 Play 管理中心在應用程式出現部分 Wake Lock 停滯時發出提醒,藉此改善應用程式效能。如果在 24 小時內,於背景發生至少一次部分 Wake Lock 並持續一小時,Android Vitals 就會報告部分 Wake Lock 停滯問題:
電池工作階段顯示的數值是應用程式所有受測量使用者的總數。如要瞭解 Google Play 如何收集 Android Vitals 資料,請參閱 Play 管理中心說明文件。
發現應用程式有部分 Wake Lock 停滯後,接下來就要解決問題。
修正問題
由於 Wake Lock 可能會耗盡裝置電池電力,因此如果有其他替代方案,請勿使用 Wake Lock。選擇正確的 API 讓裝置保持喚醒狀態說明文件可協助您為應用程式找到最佳解決方案。
如果您確實需要使用喚醒鎖定,請遵循喚醒鎖定的最佳做法,確保喚醒鎖定不會影響裝置效率。特別是,請確保您取得的每部裝置都已釋放,並盡快釋放鎖定裝置。
修正程式碼中的問題後,您可以使用本機喚醒鎖定偵錯工具驗證修正結果。
另請參閱
為您推薦
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[null,null,["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Stuck partial wake locks\n\nPartial wake locks are a mechanism in the\n[`PowerManager`](/reference/android/os/PowerManager) API\nthat lets developers keep the CPU running after a device's display turns off\n(whether due to system timeout or the user pressing the power button). Your\napp acquires a partial wake lock by calling\n[`acquire()`](/reference/android/os/PowerManager.WakeLock#acquire())\nwith the\n[`PARTIAL_WAKE_LOCK`](/reference/android/os/PowerManager#PARTIAL_WAKE_LOCK)\nflag, or by using [other APIs that acquire wake locks](/develop/background-work/background-tasks/awake/wakelock/identify-wls).\nA partial wake lock becomes *stuck* if it is held for a long time while\nyour app is running in the\nbackground (no part of your app is visible to the user). This condition drains\nthe device's battery because it prevents the device from entering lower power\nstates. Partial wake locks should be used only when necessary and released as\nsoon as no longer needed.\n\nIf your app has a stuck partial wake lock, you can use the guidance in this page\nto diagnose and fix the problem.\n\nDetect the problem\n------------------\n\nYou may not always know that your app's partial wake locks are stuck.\nIf you have already published your app,\nAndroid vitals can help make you aware of the problem.\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\nexhibiting stuck partial wake locks. Android vitals reports partial wake locks\nas stuck when at least one, hour-long, while in the background, partial wake\nlock occurs in a 24-hour period.\n\nThe number of battery sessions displayed is an aggregate for all measured users\nof the app. For information on how Google Play collects Android vitals data, see\nthe\n[Play Console](https://support.google.com/googleplay/android-developer/answer/7385505)\ndocumentation.\n\nOnce you're aware that your app has stuck partial wake locks,\nyour next step is to address the issue.\n\nFix the problem\n---------------\n\nBecause wake locks can drain the device battery, you shouldn't use wake\nlocks if there's an alternative. The\n[Choose the right API to keep the device awake](/develop/background-work/background-tasks/awake)\ndocumentation can help you find the best solution for your app.\n\nIf you do need to use a wake lock, [follow wake lock best practices](/develop/background-work/background-tasks/awake/wakelock/best-practices)\nto make sure your wake locks don't hurt device efficiency. In particular,\nmake sure every device you acquire is released, and release the lock as quickly\nas possible.\n\nAfter fixing the problem in code, you can verify your fixes by using [local\nwake lock debugging tools](/develop/background-work/background-tasks/awake/wakelock/debug-locally).\n\nSee also\n--------\n\n- [Excessive partial wake locks (beta)](/topic/performance/vitals/excessive-wakelock)\n- [Wake lock documentation](/develop/background-work/background-tasks/awake/wakelock)\n\nRecommended for you\n-------------------\n\n- Note: link text is displayed when JavaScript is off\n- [Frozen frames](/topic/performance/vitals/render#frozen-frames)\n- [Run benchmarks in Continuous Integration](/topic/performance/benchmarking/benchmarking-in-ci)\n- [Create and measure Baseline Profiles without Macrobenchmark](/topic/performance/baselineprofiles/manually-create-measure)"]]