What's the best between AlarmManager and Handler+WakeLock?

久未见 提交于 2019-12-10 09:45:24

问题


I'm using for my Android Service an Handler that reapeat some operation each 60 minutes (1 hour), with a PartialWakeLock to keep the phone not sleeping. But this cause a lot of battery usage.

So a decided to study about AlarmManager (i'm noob) that someone wrote here to be perfect for this kind of things..

But now reading along the web i find that who uses AlarmManager, still need a WakeLock. Is it true?

What is the best way to run a cycle each 60 minutes (1 hour), without kill the battery?

Thanx

P.S.

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.

But so seems that i need 2 wakelock vs just 1 wakelock using handler....is it true?


回答1:


I have made many test and this is the result:

-Alarm Manager save more battery than using handler+wakelock for long timing operation.

But you must use an additional wake lock to your activity/service started by the alarm, because the alarm manager wake lock doesn't cover it.

Even of this method uses two WakeLock the battery seems to be more efficient and with more life! During the tests (2days) the AlarmManager use 6 time less battery than other method. In my own case...

Hope this can help some one!




回答2:


I suggest you to use AlarmManager to handle events with 1 hour interval.

Because we don't know exactly what you what to achieve we can't provide a more in deep answer/suggestion sorry.




回答3:


I am not sure if it is still relevant,

but the answer is: using AlarmManager is preferred. You only need a WakeLock to keep phone running after AlarmManager has woken it up to send an Intent to your receiver and until service has finished its work. So phone will be awake only for a couple of milliseconds, compared to "all the time".



来源:https://stackoverflow.com/questions/7665713/whats-the-best-between-alarmmanager-and-handlerwakelock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!