Chheangly Prum
Chheangly Prum

Reputation: 185

Flutter: how to keep the app running on background

I'm using flutter dart for developed the for foreground app to keep the app running in background on Android. It's working fine until phone is on lock screen the app is show the notification is still running but the function won't work. Is there any solution for this issues? Thanks

Upvotes: 6

Views: 20173

Answers (2)

Aghiad Alzein
Aghiad Alzein

Reputation: 179

Try flutter_background this worked for me

Upvotes: 1

Marcel Dz
Marcel Dz

Reputation: 2714

Basically you can listen to App changes with WidgetBindingsObserver https://api.flutter.dev/flutter/widgets/WidgetsBindingObserver-class.html

A good article for this (especially lockscreen) can be found here: 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();
  }
}

Upvotes: 1

Related Questions