过度唤醒 Excessive wakeups

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,你可以用这个页面的知道来诊断并解决问题

 

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 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里面。这样好找

 

  • 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.

不要用个人的唯一定义付

  • 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.

用hardcode,不要用类名,不然混淆后难找

  • 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.

不要用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有以下好处

  • batching - jobs are combined so that battery consumption is reduced

一块干活,减少电池消耗

 

  • persistence - jobs that are marked persistent will continue to run even after the device is rebooted

设备重启也能继续干活,持续性较好

 

  • criteria - jobs can run based on conditions, such as whether or not the device is charging or WiFi is available

可以根据条件选择是否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,这样既简单又有效。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值