Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if an app running on Android?

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?

like image 355
sjor Avatar asked Nov 18 '10 08:11

sjor


2 Answers

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 } 
like image 91
dhaval Avatar answered Sep 24 '22 21:09

dhaval


isInBackground is the status of app

ActivityManager.RunningAppProcessInfo myProcess = new ActivityManager.RunningAppProcessInfo(); ActivityManager.getMyMemoryState(myProcess); Boolean isInBackground = myProcess.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND; 
like image 32
Rajneesh Shukla Avatar answered Sep 24 '22 21:09

Rajneesh Shukla