如何在 Flutter 中创建服务以使应用程序始终在后台运行? [英] How to create a service in Flutter to make an app to run always in background?
问题描述
我想让我的 Flutter 应用程序始终在后台运行.使用android,我们必须创建一个始终在后台运行的服务.我在 Flutter 文档中没有找到有关服务的内容.
I would like to make my Flutter application to run always in background. With android, we have to create a Service that runs always in background. I don't find something about Services in the Flutter documentation.
Flutter 可以做这种事情吗?
Is it possible to do this kind of things with Flutter?
推荐答案
目前没有办法直接从 flutter 做到这一点,尽管这可能会在某些时候发生变化 - 请参阅 此错误/功能请求.不过,您确实有几个选择.
There isn't a way to do this directly from flutter right now although that may change at some point - see this bug/feature request. You do have a couple of options though.
第一种是使用MethodChannels,简单的写下你要创建后台服务的android代码(或者,如果您希望它始终是一个后台服务,您可能可以在不需要颤振端通信的情况下做到这一点).
The first is to use MethodChannels and simply write the android code you want to create a background service (or if you want it to always be a background service you can probably do that without needing communication from the flutter side).
第二个是这两个插件的组合 - android_alarm_manager 和 android_intent.但这不会对所有用例都有帮助.
The second is some combination of these two plugins - android_alarm_manager and android_intent. But that won't help for all use-cases.
Flutter 现在支持运行后台进程.有关详细信息,请参阅此页面.
Flutter now supports running background processes. See this page for details.
这篇关于如何在 Flutter 中创建服务以使应用程序始终在后台运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!