In some cases you might want to check whether you app is running in background or foreground. An example use case would be, you want to show push notification message only when the app is in background. The following method will be very useful to know app running state.

First add the GET_TASKS permission to your AndroidManifest.xml

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

Then use the below method to check if the app is in background or foreground. This method will return true if the app is in background.

private boolean isAppIsInBackground(Context context) {
        boolean isInBackground = true;
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
            List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
            for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
                if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                    for (String activeProcess : processInfo.pkgList) {
                        if (activeProcess.equals(context.getPackageName())) {
                            isInBackground = false;
                        }
                    }
                }
            }
        } else {
            List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
            ComponentName componentInfo = taskInfo.get(0).topActivity;
            if (componentInfo.getPackageName().equals(context.getPackageName())) {
                isInBackground = false;
            }
        }

        return isInBackground;
    }
Author

Ravi is hardcore Android programmer and Android programming has been his passion since he compiled his first hello-world program. Solving real problems of Android developers through tutorials has always been interesting part for him.

  • brainail

    If you want to use the code like this then don’t forget to add 🙂

    • Thank you. I’ll update the article 🙂

      • Seyi

        Pease update the tutorial as GET_TASKS is deprecated. Thanks!

      • chinmay

        Google has deprecated GET_TASKS permission in API 21. can you tell me any replacement for that

  • Prabhjas Singh Bajwa

    Where to write this whole code means in which activity ?

  • Wishy Gupta

    GET_TASKS permission is deprecated in API 21, is there any other solution for it?

  • Seshu Saragadam

    How can i get installed applications running background or not??

  • Jose Omar Gonzalez

    Not works for me

  • Swati Agarwal

    Hi Ravi,
    How can we check if my application is running at all either in foreground or in background

  • sameer khader

    Hello Ravi,

    how can we check if the fragment in the activity is alive in case of foreground for push notification?

  • Swati Agarwal

    Hi Ravi,

    the permission GET_TASKS is deprecated now so on using the getRunningTasks() it gives Security exception and the application crashes.As far as i have read throughtout the internet about this, is that we can use getTasks() for API level > 21. But then what to use for API level <21 ? And in android developer itself its written that this permission and associated function should not be used for the behavioural purpose but only for testing … Can you help me with this. ?

    • Dhruv

      +1 I am dealing with the same problem.
      If you got any solution, then please help!