如何创建Flutter后台服务,该后台服务在应用程序关闭时也可以使用 [英] How to create a Flutter background service that works also when app closed

查看:283
本文介绍了如何创建Flutter后台服务,该后台服务在应用程序关闭时也可以使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为大学的学术目的开发Flutter应用程序.现在,我需要运行某种始终在线"的后台服务才能使用蓝牙,最重要的是要从外部REST API获得定期更新.

I am developing a Flutter app for academic purposes in university. Now, I need to run a sort of "always on" background service in order to use Bluetooth and most importantly to get periodic updates from external REST APIs.

我已经从pub.dev(),却找不到找到即用即用的完美解决方案(按预期...).我还看到了Kotlin的某些东西(此处).我想知道什么是实现我的目标的最佳选择(就最佳实践,完整性和简便性而言).

I've already checked those two libraries from pub.dev (this and this) without finding the perfect solution that works out-of-the-box (as expected...). I also saw something native with Kotlin (here). What I would to know is what is the best option in order to achieve my goals (in terms of best practices, completeness and with simplicity in mind).

请看下面的图片以获得更示意图:

Have a look at the following image for a more schematic view:

推荐答案

对于这种情况,您必须依靠本机机制.

For this kind of things you have to rely on native mechanisms.

在始终存在"的后台组件中处理API的最有效方法是实施推送通知服务. 使用这种技术,远程服务器可以启动与您的应用程序的通信,将其唤醒,然后执行需要执行的任务.

The most efficient way to deal with APIs in an "always existent" background component is to implement a push notification service. Using that kind of tech a remote server is capable of starting communication with your app, awaken it and then perform whatever task needs to be performed.

此外,在Android中,您还有前台服务即使关闭该应用程序也可以运行.

Also, in Android, you have foreground services which will run even if the app is closed.

这篇关于如何创建Flutter后台服务,该后台服务在应用程序关闭时也可以使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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