AlarmManager和ScheduledExecutorService的区别

编程入门 行业动态 更新时间:2024-10-08 18:38:18
本文介绍了AlarmManager和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 WakeLocks 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的区别

本文发布于:2023-11-27 11:07:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637818.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:区别   AlarmManager   ScheduledExecutorService

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!