java - AlarmManager 和 Handler+WakeLock 之间最好的是什么?

标签 java android alarmmanager standby

我正在为我的 Android 服务使用一个处理程序,它每 60 分钟(1 小时)重复一些操作,并使用 PartialWakeLock 来保持手机不 hibernate 。但这会导致大量电池使用。

所以决定研究 AlarmManager(我是菜鸟)someone wrote here非常适合这类事情..

但是现在通过网络阅读我发现使用 AlarmManager 的人仍然需要 WakeLock。是真的吗?

在不耗尽电池的情况下每 60 分钟(1 小时)运行一个循环的最佳方法是什么?

谢谢

附言

AlarmManager Android Developer

The Alarm Manager holds a CPU wake lock as long as the alarm receiver's onReceive() method is executing. This guarantees that the phone will not sleep until you have finished handling the broadcast. Once onReceive() returns, the Alarm Manager releases this wake lock. This means that the phone will in some cases sleep as soon as your onReceive() method completes. If your alarm receiver called Context.startService(), it is possible that the phone will sleep before the requested service is launched. To prevent this, your BroadcastReceiver and Service will need to implement a separate wake lock policy to ensure that the phone continues running until the service becomes available.

但是我似乎需要 2 个唤醒锁,而使用处理程序只需要 1 个唤醒锁……是真的吗?

最佳答案

我做了很多测试,结果是这样的:

-Alarm Manager 比使用 handler+wakelock 进行长时间操作更省电。

但是您必须对闹钟启动的 Activity/服务使用额外的唤醒锁,因为闹钟管理器唤醒锁不涵盖它。

即使这种方法使用两个 WakeLock 电池似乎也更高效且生命周期更长!在测试期间(2 天),AlarmManager 使用的电池比其他方法少 6 倍。就我自己而言...

希望这可以帮助到一些人!

关于java - AlarmManager 和 Handler+WakeLock 之间最好的是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7665713/

相关文章:

java - AlarmManager.ELAPSED_REALTIME_WAKEUP 在 android 中究竟做了什么

java - 有没有办法通过媒体存储访问应用程序特定的外部存储文件夹?

java - 双重检查创建单例问题的方式

android - 屏幕方向为纵向和反向纵向

android - 意外的状态行 : ICY 200 OK for URL openStream() method?

android - 程序崩溃后取消警报

android - Android 中的重复闹钟

java - 使用 itext 创建带有笛卡尔网格的 Pdf?

Java实践OCAJP混淆答案

Android AlarmManager 每 "round"10 分钟触发一次