3

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?

Community
  • 1
  • 1
Lork
  • 889
  • 3
  • 12
  • 24

3 Answers3

6

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!

Lork
  • 889
  • 3
  • 12
  • 24
  • Check https://github.com/commonsguy/cwac-wakeful/blob/master/README.markdown for a standard implementation of a service that handles its wake lock – Mr_and_Mrs_D Apr 23 '13 at 19:45
0

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

Yuriy Kulikov
  • 2,059
  • 1
  • 16
  • 29
0

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.

BrainCrash
  • 12,992
  • 3
  • 32
  • 38
  • "Almost everything you run every 1 minute" 60 MINUTE NOT SECONDS! Is true that i can use AlarmManager with phone sleeping without still using wakelock? – Lork Oct 06 '11 at 06:38
  • At the end you say: What is the best way to run a cycle each 60 seconds, without kill the battery? That got me confused... – BrainCrash Oct 06 '11 at 13:13
  • Now correct! So AlarmManager is better than Handler for this long timing operations? AlarmManager seems to use wakelock too – Lork Oct 06 '11 at 15:52
  • Yes, just use it with the RTC_WAKEUP. I don't think that wakelock is the issue here. – BrainCrash Oct 06 '11 at 17:04
  • OK, but if AlarmManager uses inside itself a wakelock (link to Android developer) and an handler timing method uses wakelock too, what's the difference for battery? – Lork Oct 06 '11 at 17:19
  • The big difference for the battery probably is what you do in your recurring task, maybe it isn't handled properly. – BrainCrash Oct 06 '11 at 19:11
  • I can't understand what you mean....if i have the SAME task, what is for the battery, the difference between AlarmManager (it use internal wakelock) and Handler (explicit wakelock) ????? Understand? – Lork Oct 08 '11 at 11:30