我正在为我的 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/