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 theRTC_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.
Wakeups 是AlarmManage里面的一个唤醒机制。可以再指定时间唤醒设备。
唤醒后,设备退出低电量模式并且在调用onReceive()或者onAlarm()的时候持有一个partial wake lock。
如果wakeup被过渡调用,电量就会很快耗尽
To help you improve app quality, Android automatically monitors apps for excessive wakeup alarms and displays the information in Android vitals. For information on how the data is collected, see Play Console docs.
安卓自动收集了这些信息并且展现在Android vitals里面。
If your app is waking up the device excessively, you can use the guidance in this page to diagnose and fix the problem.
如果你的设备过渡唤醒app,你可以用这个页面的知道来诊断并解决问题
解决问题
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 in the best practices section.
AlarmManager 是在Android 平台的早期版本引入的,随着时间推移,现在用新的api例如WorkManger也能很好的完成任务。
这个部分知识建议。长期来看,follow bestpractices
Identify the places in your app where you schedule wakeup alarms and reduce the frequency that those alarms are triggered. Here are some tips:
确定使用的位置,减少alarms 触发的频次。下面是一些建议
Look for calls to the various set()
methods in AlarmManager
that include either the RTC_WAKEUP
orELAPSED_REALTIME_WAKEUP
flag.
寻找使用set()方法的位置
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:
我们建议把包名,类名,方法名,放到tag里面。这样好找
_UNKNOWN
instead of the alarm name.不要用个人的唯一定义付
getName()
, because it could get obfuscated by Proguard. Instead use a hard-coded string.用hardcode,不要用类名,不然混淆后难找
不要用counter或者 唯一定义符
After fixing the problem, verify that your wakeup alarms are working as expected by running the following ADB command:
用下面的adb命令来确认你的wakeup alarms 工作正常
adb shell dumpsys alarm
This command provides information about the status of the alarm system service on the device. For more information, see dumpsys.
这个命令提供了挺多信息的
最佳实践:
Use wakeup alarms only if your app needs to perform an user facing operation (such as posting a notification or alerting the user). For a list of AlarmManager best practices, see Scheduling Repeating Alarms.
该用才用
Don't use AlarmManager
to schedule background tasks, especially repeating or network background tasks. UseWorkManager to schedule background tasks because it offers the following benefits
不要用AlarmManager来做后台任务,而是要用WorkManager ,因为WorkManager有以下好处
一块干活,减少电池消耗
设备重启也能继续干活,持续性较好
可以根据条件选择是否work
For more information, see Guide to background processing.
详情查看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.
不要用AlarmManager来做调度任务。 可以使用handler,这样既简单又有效。