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?
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 }
isInBackground
is the status of app
ActivityManager.RunningAppProcessInfo myProcess = new ActivityManager.RunningAppProcessInfo(); ActivityManager.getMyMemoryState(myProcess); Boolean isInBackground = myProcess.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With