What is the difference between scheduling same task using alarm manager vs ScheduledExecutorService

拟墨画扇 提交于 2019-12-10 18:39:43

问题


Hi I was reading on internet that we can schedule any thing which we want to run at a gap of certain interval using alarm manager and ScheduledExecutorService.

I want to know what is the difference between them and what to use when

Thanks in advance.


回答1:


ScheduledExecutorService is something inside your process. If your process is already around for other reasons, such as it is supplying the foreground UI, and you want to use ScheduledExecutorService for periodic work, that's fine. However, once your app moves to the background, its process may go away at any time, taking your ScheduledExecutorService with it, and you will no longer get control periodically.

AlarmManager is something outside your process. If you are looking for the Android equivalent of Windows' "Scheduled Tasks", or OS X/Linux cron jobs, that is what AlarmManager (or JobScheduler on Android 5.0+) is for. These are for cases where you need application code to run periodically in the background but are happy to allow your process to possibly be terminated in between those periods.



来源:https://stackoverflow.com/questions/28262505/what-is-the-difference-between-scheduling-same-task-using-alarm-manager-vs-sched

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!