How can I check if an app running on Android?

JavaAndroidBrowserIf Statement

Java Problem Overview


I am an Android developer and I want to write an if statement in my application. In this statement I want to check if the default browser (browser in Android OS) is running. How can I do this programmatically?

Java Solutions


Solution 1 - Java

Add the below Helper class:

public class Helper {

        public static boolean isAppRunning(final Context context, final String packageName) {
            final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            final List<ActivityManager.RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses();
            if (procInfos != null)
            {
                for (final ActivityManager.RunningAppProcessInfo processInfo : procInfos) {
                    if (processInfo.processName.equals(packageName)) {
                        return true;
                    }
                }
            }
            return false;
        }
    }

Now you can check from the below code if your desired App is running or not:

if (Helper.isAppRunning(YourActivity.this, "com.your.desired.app")) {
    // App is running
} else {
    // App is not running
}

Solution 2 - Java

isInBackground is the status of app

ActivityManager.RunningAppProcessInfo myProcess = new ActivityManager.RunningAppProcessInfo();
ActivityManager.getMyMemoryState(myProcess);
Boolean isInBackground = myProcess.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;

Solution 3 - Java

You can check it by the following method

public static boolean isRunning(Context ctx) {
    ActivityManager activityManager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);

    List<ActivityManager.RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);

    for (ActivityManager.RunningTaskInfo task : tasks) {
        if (ctx.getPackageName().equalsIgnoreCase(task.baseActivity.getPackageName()))
            return true;
    }
    return false;
}

Solution 4 - Java

Best solution is :

Create an interface like this.

interface LifeCycleDelegate {
    void onAppBackgrounded();
    void onAppForegrounded();
}

Now add a class that handle all activity lifecycle callbacks.

public class AppLifecycleHandler implements 
 Application.ActivityLifecycleCallbacks, ComponentCallbacks2 {

LifeCycleDelegate lifeCycleDelegate;
boolean appInForeground = false;

public AppLifecycleHandler(LifeCycleDelegate lifeCycleDelegate) {
    this.lifeCycleDelegate = lifeCycleDelegate;
}

@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

}

@Override
public void onActivityStarted(Activity activity) {

}

@Override
public void onActivityResumed(Activity activity) {
    if (!appInForeground) {
        appInForeground = true;
        lifeCycleDelegate.onAppForegrounded();
    }
}

@Override
public void onActivityPaused(Activity activity) {

}

@Override
public void onActivityStopped(Activity activity) {

}

@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

}

@Override
public void onActivityDestroyed(Activity activity) {

}

@Override
public void onTrimMemory(int level) {
    if (level == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
        // lifecycleDelegate instance was passed in on the constructor
        appInForeground = false;
        lifeCycleDelegate.onAppBackgrounded();
    }
}

@Override
public void onConfigurationChanged(Configuration newConfig) {

}

@Override
public void onLowMemory() {

}
}

Now in a class extending Application

public class MyApplication extends Application implements 
 LifeCycleDelegate{
 @Override
public void onCreate() {
    super.onCreate();

    AppLifecycleHandler lifeCycleHandler = new 
    AppLifecycleHandler(MyApplication.this);
    registerLifecycleHandler(lifeCycleHandler);
}

@Override
public void onAppBackgrounded() {
    Log.d("Awww", "App in background");
}

@Override
public void onAppForegrounded() {
    Log.d("Yeeey", "App in foreground");
}

private void registerLifecycleHandler(AppLifecycleHandler lifeCycleHandler) {
    registerActivityLifecycleCallbacks(lifeCycleHandler);
    registerComponentCallbacks(lifeCycleHandler);
}
}

For More refer : https://android.jlelse.eu/how-to-detect-android-application-open-and-close-background-and-foreground-events-1b4713784b57

Solution 5 - Java

fun Context.isAppInForeground(): Boolean {

val application = this.applicationContext
val activityManager = this.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
val runningProcessList = activityManager.runningAppProcesses

  if (runningProcessList != null) {
     val myApp = runningProcessList.find { it.processName == application.packageName }
     ActivityManager.getMyMemoryState(myApp)
     return myApp?.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
  }

  return false
}

Solution 6 - Java

If you are Using Kotlin

   private fun isAppRunning(context: Context, packageName: String): Boolean {
        val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        activityManager.runningAppProcesses?.apply {
            for (processInfo in this) {
                if (processInfo.processName == packageName) {
                    return true
                }
            }
        }
        return false
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsjorView Question on Stackoverflow
Solution 1 - JavadhavalView Answer on Stackoverflow
Solution 2 - JavaRajneesh ShuklaView Answer on Stackoverflow
Solution 3 - JavaAndroid DevView Answer on Stackoverflow
Solution 4 - JavaRohit LalwaniView Answer on Stackoverflow
Solution 5 - JavaSohailAzizView Answer on Stackoverflow
Solution 6 - JavaHitesh SahuView Answer on Stackoverflow