6

I've been seeing some strange issues using the Alarm manager in Android, despite the fact that I'm using RTC (non Wakeup) the phone will reliably send the PendingIntents on the correct repeating intervals.

Details of my test

  • Device is not charging, just sitting on my nightstand while I slept
  • My service woke up on its repeat interval (30 minutes, an extreme I know) EVERY TIME
  • The service logged its activity in a file so I could read it in the morning

Now from my understanding the phone should be sleeping unless I wake it up and my Alarms should not be sent until the phone is awake.

  • Why was my service executing?
  • If another service is misbehaving and using the _WAKEUP variants of the alarm will my service wake up too?
  • Can I avoid being woken by another service, and just awake from the user turning the screen on?
inazaruk
  • 74,247
  • 24
  • 188
  • 156
smith324
  • 13,020
  • 9
  • 37
  • 58

2 Answers2

4

Why was my service executing?

Presumably something else was having the device awake at those moments.

If another service is misbehaving and using the _WAKEUP variants of the alarm will my service wake up too?

Yes, though "misbehaving" is in the eye of the beholder.

Can I avoid being woken by another service, and just awake from the user turning the screen on?

Not directly via AlarmManager. You can watch for ACTION_SCREEN_OFF and ACTION_USER_PRESENT broadcasts, and perhaps disable your alarms between those.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • What about the limitations of those two broadcasts? As you noted in your bug report http://code.google.com/p/android/issues/detail?id=10735 they cannot be registered in the manifest. This is fine for an activity but because I'm dealing with a service they are essentially useless and will be released when the service dies (quickly for a IntentService). The broadcasts are also not sticky so I can't query them at will. Am I missing something? Can I have a long lived BroadcastReceiver created from inside my IntentService? – smith324 May 24 '11 at 16:40
  • @smith324: "As you noted in your bug report code.google.com/p/android/issues/detail?id=10735 they cannot be registered in the manifest." -- um, yeah, right. Sorry, forgot about that. :-( "Can I have a long lived BroadcastReceiver created from inside my IntentService?" -- an `IntentService` is not designed to be "long-lived". – CommonsWare May 24 '11 at 16:46
  • Well I guess I could create another service that listens for the screen on/off but that just feels like a hack and waste of resource :\ – smith324 May 24 '11 at 16:52
  • @smith324: Closest thing I know of to an on-demand check is `inKeyguardRestrictedInputMode()` on `KeyguardManager`. – CommonsWare May 24 '11 at 16:57
  • Thanks for that, it seems to what I need it to. – smith324 May 24 '11 at 17:44
0

I've just spent an hour trying to find out why my RTC alarm sends PendingIntents even when my phone is sleeping. And the answers is very simple, because it was pluged with USB so the phone had status "charging".

Presumably something else was having the device awake at those moments.

A lot of applications with notification ads (like AirPush, Leadbolt ect) wake up the device.

AppiDevo
  • 3,195
  • 3
  • 33
  • 51