问题描述
我在Internet上阅读,我们可以使用警报管理器和 ScheduledExecutorService
安排要在一定间隔内运行的任何事情。
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
预先感谢。
推荐答案
ScheduledExecutorService
是内部的过程。如果您的流程由于其他原因而已经存在,例如正在提供前台UI,并且您希望使用 ScheduledExecutorService
进行定期工作,那就很好。但是,一旦您的应用移至后台,它的进程就可能随时消失,将您的 ScheduledExecutorService
与它一起使用,您将不再定期获得控制权。
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
是您的过程外部。如果您正在寻找与Windows的预定任务或OS X / Linux cron
作业相对应的Android,则 AlarmManager
(或在Android 5.0及更高版本上为 JobScheduler
)适用。在某些情况下,您需要应用程序代码在后台定期运行,但很乐意允许您的进程在这两个周期之间终止。
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.
这篇关于使用警报管理器与ScheduledExecutorService计划同一任务有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!