喚醒次數過多
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
喚醒是 AlarmManager
API 的其中一種機制,可以讓開發人員設定鬧鐘,並在特定時間喚醒裝置。應用程式可以藉由在 AlarmManager
使用 RTC_WAKEUP
或 ELAPSED_REALTIME_WAKEUP
標記呼叫 set()
方法,藉此設定喚醒鬧鐘。觸發喚醒鬧鐘時,裝置會退出低電力模式,並保留部分 Wake Lock,同時執行鬧鐘的 onReceive()
或 onAlarm()
方法。如果喚醒鬧鐘的觸發次數過多,可能就會用光裝置電池電力。
為了幫助您改善應用程式品質,Android 會自動監控應用程式是否有喚醒鬧鐘次數過多的問題,並在 Android Vitals 顯示資訊。如要瞭解資料的收集方式,請參閱 Play 管理中心文件。
如果應用程式喚醒裝置的次數過多,您可以利用本頁的指南診斷並修正問題。
修正問題
AlarmManager
是
但隨著時間推移,Android 平台
先前需要的案件
AlarmManager
現已
也因為新功能的提供成效更好
WorkManager。
本章節會說明減少喚醒鬧鐘的訣竅,不過長遠來看,還是建議您遷移應用程式,以便使用「最佳做法」章節所推薦的做法。
請找出您在應用程式的哪些位置排定了喚醒鬧鐘,然後減少這些鬧鐘觸發的頻率。以下提供幾項訣竅:
修正問題後,請執行以下 ADB 指令,確認喚醒鬧鐘正常運作:
adb shell dumpsys alarm
這個指令可以提供裝置鬧鐘系統服務的狀態資訊。詳情請參閱 dumpsys。
最佳做法
只有在應用程式需要執行面向使用者的操作時 (例如發布通知或傳送快訊給使用者),才使用喚醒鬧鐘。如需 AlarmManager 的最佳做法清單,請參閱「設定鬧鐘」。
請勿使用 AlarmManager
安排背景工作時程,特別是重複性質和網路背景工作。相反地,請使用具有以下優點的 WorkManager 安排背景工作時程:
- 批次 - 整合工作並減少電池消耗量。
- 持續 - 如果裝置重新啟動,已排定的 WorkManager 工作會在重新啟動完成後執行。
- 條件 - 可以根據條件 (例如裝置是否在充電,或是否可以使用 Wi-Fi) 執行工作
詳情請參閱背景處理作業指南。
請勿使用 AlarmManager
安排只在應用程式執行期間才有效的計時作業。換句話說,在使用者退出應用程式後,就應取消計時作業。在這種情況下請使用 Handler
類別,這種類別比較容易使用,也更有效率。
為您推薦
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[null,null,["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Excessive wakeups\n\nWakeups are a mechanism in the\n[`AlarmManager`](/reference/android/app/AlarmManager) API that\nlets developers set an alarm to wake up a device at a specified time. Your app\nsets a wakeup alarm by calling one of the `set()` methods in\n[`AlarmManager`](/reference/android/app/AlarmManager) with\neither the\n[`RTC_WAKEUP`](/reference/android/app/AlarmManager#RTC_WAKEUP)\nor\n[`ELAPSED_REALTIME_WAKEUP`](/reference/android/app/AlarmManager#ELAPSED_REALTIME_WAKEUP)\nflag. When a wakeup alarm is triggered, the device comes out of low-power mode\nand holds a [partial wake lock](/topic/performance/vitals/wakelock) while executing the alarm's\n[`onReceive()`](/reference/android/content/BroadcastReceiver#onReceive(android.content.Context,%20android.content.Intent))\nor\n[`onAlarm()`](/reference/android/app/AlarmManager.OnAlarmListener#onAlarm())\nmethod. If wakeup alarms are triggered excessively, they can drain a device's\nbattery.\n\nTo help you improve app quality, Android automatically monitors apps for\nexcessive wakeup alarms and displays the information in Android vitals. For\ninformation on how the data is collected, see [Play Console\ndocs](https://support.google.com/googleplay/android-developer/answer/7385505).\n\nIf your app is waking up the device excessively, you can use the guidance in\nthis page to diagnose and fix the problem.\n\nFix the problem\n---------------\n\nThe [`AlarmManager`](/reference/android/app/AlarmManager) was\nintroduced in early versions of the Android platform, but over time, many use\ncases that previously required\n[`AlarmManager`](/reference/android/app/AlarmManager) are now\nbetter served by newer features like\n[WorkManager](/topic/libraries/architecture/workmanager).\nThis section contains tips for reducing wake up alarms, but in the long term,\nconsider migrating your app to follow the recommendations in the [best\npractices](#best_practices) section.\n\nIdentify the places in your app where you schedule wakeup alarms and reduce\nthe frequency that those alarms are triggered. Here are some tips:\n\n- Look for calls to the various\n [`set()`](/reference/android/app/AlarmManager#set(int,%20long,%20java.lang.String,%20android.app.AlarmManager.OnAlarmListener,%20android.os.Handler))\n methods in\n [`AlarmManager`](/reference/android/app/AlarmManager) that\n include either the\n [`RTC_WAKEUP`](/reference/android/app/AlarmManager#RTC_WAKEUP)\n or\n [`ELAPSED_REALTIME_WAKEUP`](/reference/android/app/AlarmManager#ELAPSED_REALTIME_WAKEUP)\n flag.\n\n- We recommend including your package, class, or method name in your alarm's tag\n name so that you can easily identify the location in your source where the\n alarm was set. Here are some additional tips:\n\n - Leave out any personally identifying information (PII) in the name, such as an email address. Otherwise, the device logs `_UNKNOWN` instead of the alarm name.\n - Don't get the class or method name programmatically, for example by calling [`getName()`](/reference/java/lang/Class#getName()), because it could get obfuscated by Proguard. Instead use a hard-coded string.\n - Don't add a counter or unique identifiers to alarm tags. The system will not be able to aggregate alarms that are set that way because they all have unique identifiers.\n\nAfter fixing the problem, verify that your wakeup alarms are working as\nexpected by running the following [ADB](/studio/command-line/adb)\ncommand: \n\n adb shell dumpsys alarm\n\nThis command provides information about the status of the alarm system service\non the device. For more information, see\n[dumpsys](https://source.android.com/devices/tech/debug/dumpsys).\n\nBest practices\n--------------\n\nUse wakeup alarms only if your app needs to perform a user facing operation\n(such as posting a notification or alerting the user). For a list of\nAlarmManager best practices, see [Scheduling\nAlarms](/training/scheduling/alarms).\n\nDon't use\n[`AlarmManager`](/reference/android/app/AlarmManager) to\nschedule background tasks, especially repeating or network background tasks. Use\n[WorkManager](/topic/libraries/architecture/workmanager)\nto schedule background tasks because it offers the following benefits:\n\n- batching - jobs are combined so that battery consumption is reduced\n- persistence - if the device is rebooted, scheduled WorkManager jobs run after the reboot finishes\n- criteria - jobs can run based on conditions, such as whether or not the device is charging or WiFi is available\n\nFor more information, see [Guide to background processing](/guide/background).\n\nDon't use [`AlarmManager`](/reference/android/app/AlarmManager)\nto schedule timing operations that are valid only while the app is running (in\nother words, the timing operation should be canceled when the user exits the\napp). In those situations, use the\n[`Handler`](/reference/android/os/Handler) class because it is\neasier to use and much more efficient.\n\nRecommended for you\n-------------------\n\n- Note: link text is displayed when JavaScript is off\n- [Stuck partial wake locks](/topic/performance/vitals/wakelock)\n- [ANRs](/topic/performance/vitals/anr)"]]