Wakeups are a mechanism in the AlarmManager API that lets developers
set an alarm to wake up a device at a specified time. Your app sets a wakeup
alarm by calling one of the set methods in AlarmManager with either
the RTC_WAKEUP or ELAPSED_REALTIME_WAKEUP flag. When a wakeup
alarm is triggered, the device comes out of low-power mode and holds a partial
wake lock while executing the alarm's onReceive or onAlarm
method. If wakeup alarms are triggered excessively, they can drain a device's
battery.
If your app is waking up the device excessively, you can use the guidance in this page to diagnose and fix the problem.
Fix the problem
The AlarmManager was introduced in early versions of the Android
platform, but over time, many use cases that previously required
AlarmManager are now better served by newer features like
WorkManager. This section contains tips for reducing wake up alarms, but in
the long term, consider migrating your app to follow the recommendations for
wakeup alarms.
Identify the places in your app where you schedule wakeup alarms and reduce the frequency that those alarms are triggered. Here are some tips:
Look for calls to the various
setmethods inAlarmManagerthat include either theRTC_WAKEUPorELAPSED_REALTIME_WAKEUPflag.We recommend including your package, class, or method name in your alarm's tag name so that you can easily identify the location in your source where the alarm was set. Here are some additional tips:
- Leave out any personally identifying information (PII) in the name, such as
an email address. Otherwise, the device logs
_UNKNOWNinstead of the alarm name. - Don't get the class or method name programmatically, for example by calling
getName(), because it could get obfuscated by Proguard. Instead use a hard-coded string. - Don't add a counter or unique identifiers to alarm tags. The system won't be able to aggregate alarms that are set that way because they all have unique identifiers.
- Leave out any personally identifying information (PII) in the name, such as
an email address. Otherwise, the device logs
After fixing the problem, verify that your wakeup alarms are working as expected by running the following ADB command:
adb shell dumpsys alarm
This command provides information about the status of the alarm system service on the device. For more information, see dumpsys.
Recommendations for wakeup alarms
Use wakeup alarms only if your app needs to perform a user facing operation (such as posting a notification or alerting the user). For a list of AlarmManager best practices, see Scheduling Alarms.
Don't use AlarmManager to schedule background tasks, especially repeating
or network background tasks. Use WorkManager to schedule background tasks
because it offers the following benefits:
- batching - jobs are combined so that battery consumption is reduced
- persistence - if the device is rebooted, scheduled WorkManager jobs run after the reboot finishes
- criteria - jobs can run based on conditions, such as whether or not the device is charging or WiFi is available
For more information, see Guide to background processing.
Don't use AlarmManager to schedule timing operations that are valid only
while the app is running (in other words, the timing operation should be
canceled when the user exits the app). In those situations, use the
Handler class because it is easier to use and much more efficient.
Recommended for you
- Note: link text is displayed when JavaScript is off
- Stuck partial wake locks
- ANRs