How to tell if android app is in the background or foreground

Cdn_EuroCdn_Euro Member ✭✭✭
edited August 2018 in Xamarin.Android

How can I detect if my Android app is in the background or foreground please?

This is to only send push notification when the application is in the background. I need to perform this check in :

public class MyFirebaseMessagingService : FirebaseMessagingService

Best Answer

  • Cdn_EuroCdn_Euro ✭✭✭
    Accepted Answer

    @izhang Thanks. In the end i used this and it worked:

    private bool isApplicationInTheBackground()
            {
                bool isInBackground;
    
                RunningAppProcessInfo myProcess = new RunningAppProcessInfo();
                ActivityManager.GetMyMemoryState(myProcess);
                isInBackground = myProcess.Importance != Android.App.Importance.Foreground;
    
                return isInBackground;
            }
    

    What do you think. I cant use the overrides, it would be trickier because i need this check in another class of the project, namely in the class containing the OnMessageReceived() method.

Answers

  • izhangizhang USMember, Xamarin Team Xamurai

    One way you can accomplish this is to create a state within your app and then toggle the state whenever onResume() or onPause() is called.

    Here's some sample code showing the general idea:
    public static bool isBackground = false;

     protected override void OnPause()
        {
            isBackground = true;
            base.OnPause();
        }      
    
     protected override void OnResume()
        {
            isBackground = false;
            base.OnResume();
        }      
    

    Reference this thread

  • Cdn_EuroCdn_Euro Member ✭✭✭
    Accepted Answer

    @izhang Thanks. In the end i used this and it worked:

    private bool isApplicationInTheBackground()
            {
                bool isInBackground;
    
                RunningAppProcessInfo myProcess = new RunningAppProcessInfo();
                ActivityManager.GetMyMemoryState(myProcess);
                isInBackground = myProcess.Importance != Android.App.Importance.Foreground;
    
                return isInBackground;
            }
    

    What do you think. I cant use the overrides, it would be trickier because i need this check in another class of the project, namely in the class containing the OnMessageReceived() method.

  • izhangizhang USMember, Xamarin Team Xamurai

    @RRazvan

    I haven't worked with this method directly but if it works then I don't think there's any issues. Based off the documents I am reading this seems like a viable way to check.

    https://developer.android.com/reference/android/app/ActivityManager.RunningAppProcessInfo

  • Cdn_EuroCdn_Euro Member ✭✭✭

    @izhang Thanks.

  • batmacibatmaci DEMember ✭✭✭✭✭

    what is your latest conclusion? did you find a good way to handle it? i also need to determine it on a sepecific page, not using app.xaml.cs ?

  • ElishaMisoiElishaMisoi Member
    edited February 5

    @izhang said:
    One way you can accomplish this is to create a state within your app and then toggle the state whenever onResume() or onPause() is called.

    Here's some sample code showing the general idea:
    public static bool isBackground = false;

    protected override void OnPause()
    {
    isBackground = true;
    base.OnPause();
    }

    protected override void OnResume()
    {
    isBackground = false;
    base.OnResume();
    }

    I think this approach is quite smart and simple... And can work for both iOS and Android.

    Can be useful when handling notifications.

  • MehulTheXammerMehulTheXammer USMember ✭✭

    Hello All,

    I am not sure if the actual title of this post was anyway related to Push Notification.

    However, if it is related to Push Notification, the best way to identify is to look into EventArgs of your listener. Whenever a notification is received in background, the Message property would always be blank/null.

    Hence you can add a check like below:

    //If app is in background
    if(e.Message == null)
    {
        // perform actions for app background state
    }
    
Sign In or Register to comment.