4

Could someone explain me different situations to use AlarmManager vs Handler with examples please. Any disadvantages of using these two as alternate to each other?

Thanks.

Ravi Rode
  • 23
  • 5
poddroid
  • 845
  • 1
  • 14
  • 26
  • If you are planning to use the AlarmManager, check out the BuzzBox SDK: it is built on top of it and you can schedule a Task using a cron expression. Let me know what you think: http://hub.buzzbox.com/android-sdk/ – robsf Mar 28 '11 at 17:03

1 Answers1

13

They have little to do with one another. I am assuming you are referring to using something like postDelayed() on Handler for polling, which is but one small feature of Handler.

You would use postDelayed() (also available on any widget or other subclass of View) in an activity for simple timing events that are within the activity itself.

You would use AlarmManager for periodic background operations, much like you would use cron in Linux/OS X or a Scheduled Task on Windows. For example, if you were writing an email client, you would use AlarmManager to trigger your code that calls the mail server and checks for new messages. The user could choose the email-check frequency, which would determine how frequently AlarmManager would trigger your code.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I've been looking for this answer for days; thx. Is it true that Handler will not execute postDelayed if the phone is off (screen dark), but AlarmManager will? I have polling code that works when the phone is kept permanently on, but not when the phone sleeps (or whatever you call it when the screen is off but the phone is still on); want to know if I should be hunting another bug or if swapping for AlarmManager will possibly address the issue. – kyle May 10 '12 at 16:40
  • 4
    @kyle: When the phone is asleep, nothing runs, including any `postDelayed()` stuff. `AlarmManager` can wake up the phone if used correctly. – CommonsWare May 10 '12 at 16:46