WorkManager vs AlarmManager,根据情况使用什么 [英] WorkManager vs AlarmManager, what to use depending on the case

查看:64
本文介绍了WorkManager vs AlarmManager,根据情况使用什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须执行这个用例(不是代码,只是正确的用法)用例:我需要每天在 00:30 从网络获取一些数据.这些数据为我提供了一些特定的时间,其中一个是 4:30 左右(每天变化 +1 分钟 -1 分钟,取决于服务器响应,不能在任何地方使用 ++ 或 -- 逻​​辑).在这个 (4:30) 上,我需要安排一个 Alarm .什么不清楚:

I have to perform this use case (not code , just the right usage) The use case : I need to fetch some data from the network everyday at 00:30 . These data provide me some specific times , and one of them is around 4:30 (changes everyday by +1 minute -1 minute , depends on the server response, can't use ++ or -- logic anywhere) . On this one (4:30), I need to schedule an Alarm . What is unclear :

我应该为此直接使用 AlarmManager 吗?

我应该使用 WorkManager 来获取需要报警的时间而不是使用 AlarmManager 吗?

我应该只使用 WorkManager 吗?

我之所以困惑,是因为我读过的一些博客说,如果我在特定时间有一些工作,最好坚持使用AlarmManager,但我仍然可以使用WorkManager

The reason why I am confused is because some blogs I have read say that is better to stick to AlarmManager if I have some work at a specific time, but still, I can do it with WorkManager

这是怎么做到的?

推荐答案

我应该为此直接使用 AlarmManager 吗?

Should I use AlarmManager directly for this ?

是的,你应该这样做.据我所知,AlarmManager 是处理像您这样的任务的最佳选择,也是处理 打盹模式.使用第一个闹钟在特定时间设置第二个闹钟.

Yes you should. AlarmManager is the best option as far as I know to handle tasks like yours and also is the safer option when dealing with doze mode. Use the first alarm to set the second alarm at a specific time.

我应该使用 WorkManager 来获取需要报警的时间而不是使用 AlarmManager 吗?

Should I use WorkManager to get the time when I need to alarm and than use AlarmManager ?

如果你想使用这种方法,你需要调用 AlarmManager 并在 WorkManager 上调度一个 Worker.WorkManager 需要在特定时间之前运行,并不能保证 Worker 在 4.30 之前完成或将执行.

If you want to use this approach you need to call AlarmManager and dispatch a Worker on WorkManager. The WorkManager need to run before a specific time and is not guaranteed that the Worker finish or will be executed before 4.30.

我感到困惑的原因是因为我读过的一些博客说如果我在特定时间有一些工作,最好坚持使用AlarmManager,但我仍然可以使用WorkManager

The reason why I am confused is because some blogs I have read say that is better to stick to AlarmManager if I have some work at a specific time, but still, I can do it with WorkManager

WorkManager 不保证执行时间.将来它可能可以做到这一点.

WorkManager doesn't guarantee the time of execution. It probably can do this in the future.

我应该只使用 WorkManager 吗?

Should I just use WorkManager ?

不,出于之前所述的原因.如果您想使用作业调度程序,Android-Job 是您的用例的短期响应.如果您转到链接,您还可以看到功能和差异表.

No, for the reasons expressed before. Android-Job is the short term response of your use case if you wanna use a job scheduler. Also you can see a table of features and differences if you go to the link.

编辑 17/03/2020:Android-Job 已弃用.

这篇关于WorkManager vs AlarmManager,根据情况使用什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆