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

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

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值