Search code examples
javaandroidalarmmanagerstandby

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


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?


Solution

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