在Android上定期安排后台任务

发布于 2025-01-10 16:57:43 字数 828 浏览 1 评论 0原文

我正在开发一个应用程序,我必须从多个传感器读取数据并每 15 分钟将其发送到远程服务器。这也必须在应用程序关闭/终止时完成,并且我还必须确保它始终执行。我还想确保它(几乎)恰好每 15 分钟发生一次(+-1 分钟差异是上限)。

此时,我找到了 3 个选项:使用 Workmanager、Alarmmanager 或使用前台服务。遵循文档,Workmanager 似乎是执行后台任务的方法,但是,在阅读了一些内容之后, Alarmmanager 似乎是一个更安全的选择(Workmanager 有时在打瞌睡模式下会出现问题,并且时间不准确,因为 它使用至少 5 分钟的弹性周期)。并且 前台服务 并不是真正允许执行此类任务的(实际上并不是这样)长时间运行,这只是一个周期性任务)并且在较新的 Android 版本中受到限制。您认为使用 Alarmmanger 来完成此任务是个好主意,还是应该使用其他东西?谢谢!

I'm working on an app where I have to read data from multiple sensors and send it to a remote server every 15 minutes. This has to be done when the app is closed/killed as well and I also have to be sure it always executes. I also want to be sure it happens (almost) exactly every 15 minutes (+-1 minute difference is the upper limit).

At this point, I've found 3 options: using Workmanager, Alarmmanager or using a foreground service. Following the documentation, Workmanager seems the way to go for background tasks, however, after having done some reading, Alarmmanager seems to be a safer choice (Workmanager sometimes has troubles with doze mode, and the timing isn't exact because it uses a flex period of at least 5 minutes). And a foreground service is not really allowed for this kind of task (it's not really long running, it's just a periodic task) and is being limited in newer Android versions. Do you think it would be a good idea to use an Alarmmanger for this task, or should I use something else? Thanks!

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

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

发布评论

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

评论(1

萌无敌 2025-01-17 16:57:44

TODO 后台调度.. 您可以使用此方法来做您的事情..

KOTLIN;

val service = Executors.newSingleThreadScheduledExecutor()
        val handler = Handler(Looper.getMainLooper())
        service.scheduleAtFixedRate({
            handler.run {
                // Do your stuff here, It gets loop every 15 Minutes
            }
        }, 0, 15, TimeUnit.MINUTES);

JAVA;

  ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
        Handler handler = new Handler(Looper.getMainLooper());

        service.scheduleAtFixedRate(() -> {
            handler.post(() -> {
                // Do your stuff here, It gets loop every 15 Minutes
            });
        }, 0, 15, TimeUnit.MINUTES);

TODO Background scheduling.. You can use this method todo your stuff..

KOTLIN;

val service = Executors.newSingleThreadScheduledExecutor()
        val handler = Handler(Looper.getMainLooper())
        service.scheduleAtFixedRate({
            handler.run {
                // Do your stuff here, It gets loop every 15 Minutes
            }
        }, 0, 15, TimeUnit.MINUTES);

JAVA;

  ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
        Handler handler = new Handler(Looper.getMainLooper());

        service.scheduleAtFixedRate(() -> {
            handler.post(() -> {
                // Do your stuff here, It gets loop every 15 Minutes
            });
        }, 0, 15, TimeUnit.MINUTES);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文