且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用警报管理器与ScheduledExecutorService计划同一任务有什么区别

更新时间:2023-01-12 18:03:29

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.