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

时间:2017-08-04 12:11:49

标签: android alarmmanager handler

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.

2 个答案:

答案 0 :(得分:0)

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.

答案 1 :(得分:-1)

Previously SyncAdapters were used for performing long performing operations, Now JobSchedular is the latest background monitoring service which can be used.