立即执行后台代码,我的应用程序完全终止,再也不会

时间:2021-06-15 06:44:33

标签: android firebase flutter dart google-cloud-functions

我正在实施我的群组语音通话应用的在线/离线功能。当用户按下菜单(打开设备上所有打开的应用程序)并滑动我的应用程序时,我想从活动用户电梯中删除用户。所以我可以触发一个 firebase 函数来删除。现在我只能处理 willpop 和应用程序在后台时。

2 个答案:

答案 0 :(得分:0)

尝试在 dispose 方法中编写该代码。

答案 1 :(得分:0)

您应该使用“didChangeAppLifecycleState

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
  @override
  void initState() {
    super.initState();
    // add the observer
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    // remove the observer
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);

    // These are the callbacks
    switch (state) {
      case AppLifecycleState.resumed:
        // widget is resumed
        break;
      case AppLifecycleState.inactive:
        // widget is inactive
        break;
      case AppLifecycleState.paused:
        // widget is paused
        break;
      case AppLifecycleState.detached:
        // widget is detached
        break;
    }
  }

  @override
  Widget build(BuildContext context) => Scaffold();
}

AppLifecycleState.paused 上始终使用户处于非活动状态 并在 AppLifecycleState.resumed

中再次激活用户