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

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!

      • Kcrik

        Use the Lifecycle callback in your Application.
        Then in onResume(), do
        if (LifeCycleHandler.paused == LifeCycleHandler.stopped)
        // Application coming back from background

  • biruk tsegaye

    Thank you very much, it helps me a lot

  • Meet Dhamsania

    How to check any other application is launched or not and in which class we should write this.. how to call this class.. can anyone explain please?

  • Kcrik

    People, don’t listen to that article, it’s a REALLY bad way to do things. You shouldn’t use the PackageManager.

    For any body, just use the Application.ActivityLifecycleCallbacks to hook up how many activities have been started, stopped, paused and resumed.

    Then in the onResume() function of the activity you want to check whether you’re coming back from the background or not, do

    public void onResume()

    if (LifeCycleHandler.paused == LifeCycleHandler.stopped)
    // Coming back from background