部分唤醒锁定操作卡住
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
部分唤醒锁定是 PowerManager
API 中的一种机制,可让开发者在设备的显示屏关闭后(无论是由于系统超时还是用户按下电源按钮)继续保持 CPU 运行。您的应用可以通过调用带有 PARTIAL_WAKE_LOCK
标志的 acquire()
或使用其他用于获取唤醒锁定的 API来获取部分唤醒锁定。当您的应用在后台运行时,如果部分唤醒锁定保持了较长时间,则会变为卡住状态(用户看不到应用的任何部分)。这种情况会耗尽设备的电量,因为它会阻止设备进入低功耗状态。部分唤醒锁定仅应在必要时使用,并且在不再需要时立即释放。
如果应用的部分唤醒锁定被卡住,您可以使用本页中的指南来诊断和解决问题。
检测问题
您有时可能并不知道应用的部分唤醒锁定卡住了。如果您已发布了应用,Android Vitals 可以帮助您认识到这个问题。
Android Vitals
当您的应用出现部分唤醒锁定卡住时,Android Vitals 可通过 Play 管理中心提醒您,从而帮助您改进应用性能。Android Vitals 报告部分唤醒锁定卡住的条件是在 24 小时内后台至少发生了一次时长达 1 小时的部分唤醒锁定。
显示的电池工作时段是针对应用的所有测量用户的汇总值。如需了解 Google Play 如何收集 Android Vitals 数据,请参阅 Play 管理中心文档。
当您发现应用存在部分唤醒锁定卡住时,下一步就是解决问题。
解决问题
由于唤醒锁定可能会耗尽设备电池电量,因此如果有替代方案,请勿使用唤醒锁定。选择合适的 API 来保持设备唤醒状态文档可帮助您为应用找到最佳解决方案。
如果确实需要使用唤醒锁定,请遵循唤醒锁定最佳实践,确保唤醒锁定不会降低设备效率。特别是,请确保释放您获取的每部设备,并尽快释放锁定。
修复代码中的问题后,您可以使用本地唤醒锁定调试工具验证您的修复。
另请参阅
为您推荐
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):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)"]]