【问题标题】:Flutter: how to keep the app running on backgroundFlutter:如何让应用程序在后台运行
【发布时间】:2020-09-27 00:43:06
【问题描述】:

我正在使用颤振飞镖开发前台应用程序,以保持应用程序在 Android 上的后台运行。在手机处于锁定屏幕上之前它工作正常,应用程序显示通知仍在运行,但该功能将不起作用。 这个问题有什么解决办法吗?谢谢

【问题讨论】:

标签: android flutter dart foreground-service


【解决方案1】:

基本上你可以用WidgetBindingsObserver收听App的变化 https://api.flutter.dev/flutter/widgets/WidgetsBindingObserver-class.html

可以在这里找到一篇很好的文章(尤其是锁屏): https://medium.com/@tomalabasteruk/flutter-in-app-lock-screens-f6a17fa02af

class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
  AppLifecycleState _notification; 
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    setState(() {
      _notification = state;
    });
  }

  @override
  initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
    ...
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }
}

【讨论】:

    【解决方案2】:

    试试flutter_background 这对我有用

    【讨论】:

      猜你喜欢
      • 2022-09-24
      • 2019-04-26
      • 1970-01-01
      • 2015-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多