唤醒次数过多
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
唤醒是 AlarmManager
API 中的一种机制,可让开发者设置闹钟以在指定时间唤醒设备。为设置唤醒闹钟,您的应用会调用 AlarmManager
中某个带有 RTC_WAKEUP
或 ELAPSED_REALTIME_WAKEUP
标志的 set()
方法。当唤醒闹钟触发时,设备会在执行闹钟的 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 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):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)"]]