哪个是最好的 AlarmManager 或 Handler post 重复后台任务的延迟

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

我必须每半小时重复一次天气 API 任务以从 http://api.openweathermap.org 获取数据,因为我使用了处理程序 post 延迟,但有人建议我这样做使用警报管理器重复任务,因为它会产生中断。但如果我们使用处理程序,它会消耗更多内存并使用更多 Cpu。我需要建议哪一个最好。

AlarmManager 的文档指出了何时使用它。

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.

在你的情况下,你的应用程序不会一直可见,因为它是一个天气应用程序。在这里使用 AlarmManager 来更新数据是有意义的。对于更多的赞成和反对,有更多的答案here

始终视情况而定。

以前 SyncAdapters 用于执行长时间执行的操作,现在 JobSchedular 是可以使用的最新后台监控服务。