Flutter:如何使应用程序在后台运行

问题描述 投票:0回答:1

我正在使用Flutter dart开发for前台应用程序,以使该应用程序在Android上在后台运行。直到手机处于锁定屏幕,应用程序显示通知仍在运行,但该功能无法正常工作之前,它都可以正常工作。这个问题有解决方案吗?谢谢

android flutter dart foreground-service
1个回答
0
投票

基本上,您可以使用WidgetBindingsObserver收听应用更改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();
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.