在本機偵錯 Wake Lock
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
您可以使用多種工具,對使用喚醒鎖定功能在本機執行的應用程式進行偵錯。這些工具可協助您找出並修正效能問題。
下列工具可協助您偵錯或最佳化喚醒鎖:
- dumpsys 可提供裝置系統服務的狀態資訊。
- 系統追蹤會產生追蹤記錄檔案,可用於產生系統報告。
- Android Studio 的背景工作檢查器可協助您監控喚醒鎖定,包括可能由 WorkManager 等程式庫取得的喚醒鎖定。
dumpsys
dumpsys 是執行於 Android 裝置上的工具,可提供裝置系統服務的相關資訊。
下列指令特別適合用於偵錯喚醒鎖定:
系統追蹤
系統追蹤會記錄短時間內的各種裝置活動,系統追蹤會產生追蹤記錄檔案,可用於產生系統報告。這份報表可協助您找出改善應用程式效能的方法。
如要瞭解如何開始使用,請參閱這份系統追蹤快速入門指南。你也可以觀看這部影片,瞭解如何提升 Android 電池效率。
背景工作檢查器
您可以使用 Android Studio 的背景工作檢查器,監控喚醒鎖定、鬧鐘和工作。
具體來說,WorkManager 程式庫會使用 JobScheduler 排定及執行工作。這些工作執行時,會保留歸給應用程式的喚醒鎖定。您可以使用「Background Task Inspector」監控在背景執行的工作站和工作,並查看工作詳細資料。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-08-27 (世界標準時間)。
[null,null,["上次更新時間:2025-08-27 (世界標準時間)。"],[],[],null,["There 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\ndumpsys\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\nSystem 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\nBackground 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."]]