Search code examples
androidalarmmanagerhandler

which one is best AlarmManager or Handler post delay for repeating background task


I have to repeat a weather API Task after every half an hour to fetch data from the http://api.openweathermap.org for that I have used handler post delay but some one suggests me to use Alarm manager for repeating task as It produces interrupt. but if we use handler it consume more memory and uses more Cpu. I need suggestion which one is best.


Solution

  • The docs for AlarmManager point it out when to use it.

    Note: The Alarm Manager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running. For normal timing operations (ticks, timeouts, etc) it is easier and much more efficient to use Handler.

    In your case your app will not be visible all the time since it's a weather app. It makes sense to use AlarmManager here to update the data. For more pro's and con's there are more answers here

    It's always case dependent.