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

时间:2020-06-08 05:19:23

标签: android flutter dart foreground-service

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

1 个答案:

答案 0 :(得分: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();
  }
}