AlarmManager和ScheduledExecutorService的区别 [英] Difference between AlarmManager and ScheduledExecutorService
问题描述
除了设置和详细的时间(即午夜)与设定的延迟(即24小时),什么是 AlarmManager
两者的区别和 ScheduledExecutorService的
来定期运行任务?
Besides setting and exact time (i.e. midnight) versus setting a delay (i.e. 24 hours), what's the difference between using AlarmManager
and ScheduledExecutorService
to run a task periodically?
在我的情况,我需要运行的code一点点地检查是否有新的数据,每天晚上,并创建一个新的通知,如果有新的数据。
In my case, I need to run a little bit of code to check for new data every night and create a new notification if there is new data.
谢谢!
推荐答案
ScheduledExecutorService的
运行在你的应用程序。如果应用程序进程死掉了,没有任何的计划任务运行。因此,需要服务
(让你的生活过程中的活动以外生命周期的积极作用)。
ScheduledExecutorService
runs in your application process. If application process dies, none of the scheduled tasks will run. Hence the need for Service
(so your process lives beyond Activities active part of lifecycle).
在 AlaramManager
是运行所有的时间关键的系统服务。如果您的应用程序计划的东西,被杀害了,然后 AlarmManager
可以再次(通过 PendingIntent
)启动应用程序。
While AlaramManager
is critical system service that runs all the time. And if your application scheduled something and was killed, then AlarmManager
may start application again (via PendingIntent
).
和最后一个主要的区别,没有人在这里提到的是, AlarmManager
关于 WakeLock
知道S和电源管理。这意味着 AlaramManager
可Android设备唤醒在指定时间运行计划任务。虽然 ScheduledExecutorService的
一窍不通的电源管理,并且只启动任务时,设备不处于深度睡眠(即它可以简单地错过时间)。
And the last major difference that no one mentioned here is that AlarmManager
knows about WakeLock
s and power management. This means that AlaramManager
may wake up Android device at specified time to run scheduled task. While ScheduledExecutorService
knows nothing about power management and will only start task when device is not in deep sleep (i.e. it can simply miss the time).
这篇关于AlarmManager和ScheduledExecutorService的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!