在本地调试唤醒锁定
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
您可以使用多种工具来调试使用唤醒锁的本地运行应用。这些工具可帮助您识别和解决性能问题。
以下工具可帮助您调试或优化唤醒锁定:
- dumpsys 提供有关设备上系统服务状态的信息。
- 系统跟踪会生成轨迹文件,该文件可用于生成系统报告。
- Android Studio 后台任务检查器可帮助您监控唤醒锁,包括可能由 WorkManager 等库获取的唤醒锁。
dumpsys
dumpsys 是一种在 Android 设备上运行的工具,可提供有关设备系统服务的信息。
以下命令对于调试唤醒锁定特别有用:
adb shell dumpsys batterystats
提供了每个应用持有的唤醒锁的详细历史记录。如需了解详情,请参阅 dumpsys 检查电池诊断信息文档。
系统跟踪
系统跟踪可在短时间内记录各种设备活动。系统跟踪会生成轨迹文件,该文件可用于生成系统报告。此报告有助于您了解如何提升应用的性能。
如需了解如何开始使用,请参阅此系统跟踪快速入门指南。您还可以观看此视频,了解如何提高 Android 电池使用效率。
后台任务检查器
您可以使用 Android Studio 的后台任务检查器监控唤醒锁、闹钟和作业。
具体来说,WorkManager 库使用 JobScheduler 来调度和执行作业。在这些作业运行时,它们会持有归因于应用的唤醒锁。您可以使用后台任务检查器监控在后台执行的工作器和作业,并查看有关其工作的详细信息。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-21。
[null,null,["最后更新时间 (UTC):2025-08-21。"],[],[],null,["# Debug wake locks locally\n\nThere are a number of tools you can use to debug a locally-running app that uses\nwake locks. These tools can help you identify and fix performance issues.\n| **Note:** [Some APIs acquire wake locks that are attributed to your app](/develop/background-work/background-tasks/awake#actions-keep). This means your app might be using wake locks even though you aren't writing that code explicitly. If your app has mysterious performance issues, it can be helpful to check if there are misbehaving wake locks. If your app is holding wake locks and you don't recognize the names, [Identify wake locks created by other APIs](/develop/background-work/background-tasks/awake/wakelock/identify-wls) can help you identify the API that might have created them.\n\nThe following tools can help you debug or optimize your wake locks:\n\n- [dumpsys](#dumpsys) provides information about the status of system services on a device.\n- [System tracing](#system-tracing) produces a trace file that you can use to generate a system report.\n- The Android Studio [Background Task Inspector](#bg-task) helps you to monitor wake locks, including wake locks that might be acquired by libraries like WorkManager.\n\n### dumpsys\n\n[dumpsys](/tools/dumpsys) is a tool that runs on Android devices and provides\ninformation about the device's system services.\n\nThe following command is particularly useful for debugging wake locks:\n\n- `adb shell dumpsys batterystats` provides a detailed history of wake locks held by each app. For more information, see the dumpsys [Inspect battery\n diagnostics](/tools/dumpsys#battery) documentation.\n\n### System tracing\n\n[System tracing](/topic/performance/tracing) records a wide range of device activity\nover a short period. System tracing produces a *trace file* that you can use to\ngenerate a system report. This report helps you identify ways to improve your\napp's performance.\n\nFor information on how to get started, see this [system tracing quickstart\nguide](https://perfetto.dev/docs/quickstart/android-tracing). You can also watch [this video on improving\nAndroid battery efficiency](https://youtu.be/jS46zP8kQ3k?si=BCteWawO-rK7EAGl).\n\n### Background Task Inspector\n\nYou can use Android Studio's\n[Background Task Inspector](/studio/inspect/task) to monitor [wake locks,\nalarms and jobs](/studio/inspect/task#inspect-jobs-alarms-wakelocks).\n\nIn particular, the WorkManager library uses JobScheduler to schedule and execute\njobs. While these jobs are running, they hold a wake lock that is attributed\nto the app. You can use Background Task Inspector to monitor workers and jobs\nthat execute in the background and see details about their work."]]