2

I'm using alarm manager in my service to set non-waking alarms every 15 seconds to execute a certain task. I don't want to wake the phone up as the task isn't time critical, so i'm using the ELAPSED_REALTIME flag to set the alarm. Here's the code:

alarm.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), 15 * 1000, intentRecurringChecks);

What I'm noticing in my logs is that the task is getting executed every 15 seconds. Does this mean the phone is staying awake even though it's screen has been turned off for half an hour? Is there a way I can be sure my application's not the one waking up the phone?

I've search about this topic but I can't find a proper answer.

Thanks for your help.

black
  • 781
  • 1
  • 7
  • 22

1 Answers1

0

First, you shouldn't use AlarmManager for such timeouts. This is explicitly mentioned in documentation (read the bold part). It's better to use Handler based timers in your case. Here is an example: Repeat a task with a time delay?.

Second, when device is connected via USB, its not going to deep sleep mode. You should disconnect your device wait a minute or two. Attach it back and analyze the logs.

Community
  • 1
  • 1
inazaruk
  • 74,247
  • 24
  • 188
  • 156
  • I'm using such a short timeout just for testing purposes. Nevertheless, what's the reason for this behavior. Moreover, I'm logging in a file on android, not through USB. – black Jan 11 '12 at 17:32
  • 1
    @Sebouh: "I'm using such a short timeout just for testing purposes" -- please don't. Use something like five minutes, *then* see what happens. – CommonsWare Jan 11 '12 at 17:48
  • I checked with a 5 min delay, it sometimes executes in 5 mins, other time 5 min +/- 10 secs. Do you think this is acceptable? – black Jan 11 '12 at 18:55
  • Further update: My phone was in airplane mode for 8 hrs when I went to bed. In the morning I took a look at the logs, it was still executing the task every 5 mins except for one case where it executed 3 mins 10 secs later. I somehow find it strange that the task was never executed say 6 or 7 mins later at some point due to the phone being sleep. Can anyone please shed a light on what's going on or if this is normal? Thanks. – black Jan 12 '12 at 07:28