0

I want to build an alarm application. I've seen some examples and some of them use Service and some use BroadcasterReceiver. The user will set the alarm and then when it goes off they'll have to do certain things like solve a mathematical equation or scan NFC tag before it turns off. Which one should I use?

Johnathan Au
  • 5,244
  • 18
  • 70
  • 128

1 Answers1

3

If you are using AlarmManager with a _WAKEUP alarm, you must have the PendingIntent route to a BroadcastReceiver. The only thing Android guarantees with a _WAKEUP alarm is if you use a BroadcastReceiver, Android will keep the device awake long enough for onReceive() to complete. Anything else, all bets are off.

It the work you want to do would take more than a couple of milliseconds, have the BroadcastReceiver turn around and pass control to a service, which can do its work on a background thread. You may wish to use my WakefulIntentService for that; if not, you will need to manage your own WakeLock to ensure that the device stays awake until the service can complete its work.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I'm slightly confused. Are you saying that _WAKEUP only works if it's sent to a BroadcastReceiver, or are you saying that _WAKEUP *can* be sent to a service, but there's no guarantee that the device will stay awake long enough to do anything useful? Anyway, +1 for linking to WakefulIntentService. – Edward Falk Apr 18 '13 at 22:32
  • @EdwardFalk: Well, your second statement is more accurate, though I consider them to be more or less equivalent. What's the point of `_WAKEUP` if it will be unreliable? – CommonsWare Apr 18 '13 at 23:07
  • True, although arguably the service will *eventually* execute, the next time the device wakes up for real. – Edward Falk Apr 18 '13 at 23:32