欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

How do I check if the Flutter application is in the foreground or not?

程序员文章站 2022-04-24 16:45:35
...

I don't want to show notification when the app is in foreground. How can I check live state of my app?

Geofencing

A sample geofencing plugin with background execution support for Flutter.

Getting Started

This plugin works on both Android and iOS. Follow the instructions in the following sections for the platforms which are to be targeted.

Android

Add the following lines to your AndroidManifest.xml to register the background service for geofencing:

<receiver android:name="io.flutter.plugins.geofencing.GeofencingBroadcastReceiver"
    android:enabled="true" android:exported="true"/>
<service android:name="io.flutter.plugins.geofencing.GeofencingService"
    android:permission="android.permission.BIND_JOB_SERVICE" android:exported="true"/>

Also request the correct permissions for geofencing:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

Geofencing

A sample geofencing plugin with background execution support for Flutter.

Getting Started

This plugin works on both Android and iOS. Follow the instructions in the following sections for the platforms which are to be targeted.

Android

Add the following lines to your AndroidManifest.xml to register the background service for geofencing:

<receiver android:name="io.flutter.plugins.geofencing.GeofencingBroadcastReceiver"
    android:enabled="true" android:exported="true"/>
<service android:name="io.flutter.plugins.geofencing.GeofencingService"
    android:permission="android.permission.BIND_JOB_SERVICE" android:exported="true"/>

Also request the correct permissions for geofencing:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

4 Answers

In your State<...> class you need to implement WidgetsBindingObserver interface and listen for widget state changes. Something like this:

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();
  }
}

Then when you want to know what is the state, check _notification.index property. _notification == null => no state changes happened, 0 - resumed, 1 - inactive, 2 - paused.

share  improve this answer  follow 

answered Oct 19 '18 at 10:55

 

Valentina Konyukhova

1,79311 gold badge1414 silver badges2525 bronze badges

  • 2

    Do I need to add WidgetsBindingObserver in all screen widget classes or only in a widget which is attached to the main class? – Ameer Nov 21 '19 at 9:13

  • 1

    You need to add with WidgetsBindingObserver to the widgets in which you want to do some reaction on AppLyfecycle events – Valentina Konyukhova Dec 19 '19 at 13:32

  • The example is working as expected but I also get a message when the app is resumed: E/BpSurfaceComposerClient(10905): Failed to transact (-1). What may be the cause ? – cwhisperer Mar 18 at 9:51

  • 1

    You can always use enums with this as well (recommended) AppLifecycleState.resumed for example – Oliver Dixon Jul 17 at 12:38

  • Can we asume that the initial state is resumed? – Diego Garcia Oct 8 at 17:56

show 1 more comment

24

 

To extend on the above answer, you can use a switch statement to make it nice and neat:

 @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch(state){
      case AppLifecycleState.resumed:
        print("app in resumed");
        break;
      case AppLifecycleState.inactive:
        print("app in inactive");
        break;
      case AppLifecycleState.paused:
        print("app in paused");
        break;
      case AppLifecycleState.detached:
        print("app in detached");
        break;
    }
}

share  improve this answer  follow 

edited Oct 29 at 21:31

answered May 28 at 20:32

How do I check if the Flutter application is in the foreground or not?

crushman

33833 silver badges1919 bronze badges

  • 1

    you forgot to close curly braces at the end. Please update the answer. Thanks. – Kamlesh Oct 29 at 7:49

add a comment

1

 

Simply create a bool variable which will keep track of all your background/foreground stuff.

Full code:

class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
  // This variable will tell you whether the application is in foreground or not. 
  bool _isInForeground = true;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }
  
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    _isInForeground = state == AppLifecycleState.resumed;
  }

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

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

share  improve this answer  follow 

answered Nov 10 at 10:13

 

CopsOnRoad

80.1k2121 gold badges266266 silver badges197197 bronze badges

add a comment

0

 

My Answer may be late but may be helpful for someone. I also faced this problem there is a plugin which works on both android and ios

https://pub.dev/packages/flutter_app_lock

Where you can put you desired lockscreen also it is very flexible.

Hope someone who stuck like me can get this easily done.

相关标签: flutter dart