为后台任务选择哪一个 - 具有重复任务的计时器还是具有循环的服务?

发布于 2025-01-06 10:42:27 字数 161 浏览 1 评论 0原文

当我在应用程序中收到广播时,我有一个重复的任务(任务 A)要做。我需要知道这些选项中哪一个更好地完成重复任务?

  1. 独立类中的计时器以及任务 A 是否重复计时器到期?
  2. 一个android后台服务正在执行任务A。

选择上述方法有何优缺点?

I have a repeated task(Task A) to do when I receive a broadcast in my application. I need to know which one is better between these options to do the repeated task?

  1. A Timer in a standalone class and does the Task A repeatedly on timer expiry?
  2. An android background service doing the Task A.

what are the pros and cons in choosing the above methods?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

春风十里 2025-01-13 10:42:27

选择上述方法有何优缺点?

您似乎假设所有可能的“任务 A”实现都是平等的。您似乎还假设所有周期(一毫秒到一世纪)都是平等的。这些都不是真的。

不可能抽象地回答你的问题,要解释所有的可能性需要几十页纸。

我的任务需要更新我的 java 类变量的位置,该变量不是活动,并且必须使用 HTTPS 更新到网络服务器

如果您需要定期使用设备位置更新 Web 服务器,并且轮询周期合理(例如,每个30 分钟),最好的选择是使用 AlarmManagerService。我编写了一个 LocationPoller ,它是专为这种情况设计的,另一个开发人员改进了。请记住,可能无法在任何给定时刻确定设备的位置,因此您需要有一个“超时”机制,以防找不到位置,因为找到位置会占用 CPU(和 GPS 无线电、如果相关)已通电。 LocationPoller就有这样的“超时”。

what are the pros and cons in choosing the above methods?

You seem to assume that all possible "Task A" implementations are created equal. You also seem to assume that all periods (one millisecond to one century) are created equal. Neither of these are true.

It is impossible to answer your question in the abstract, and it would take a few dozen pages to explain all the possibilities.

My task need to update the location to my java class variable which is not an activity and has to update to a webserver using HTTPS

If you need to periodically update a Web server with the device location, and the polling period is sensible (e.g., every 30 minutes), your best option is to use AlarmManager and a Service. I wrote a LocationPoller which was designed for this scenario, which another developer has improved upon. Just bear in mind that it may not be possible to determine the device's location at any given moment, so you need to have a "timeout" mechanism in case you cannot find the location, since finding the location keeps the CPU (and GPS radio, where relevant) powered on. LocationPoller has such a "timeout".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文