问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/5448758/different-situations-to-use-alarmmanager-vs-handler-android