How to Check If an App Running on Android

How can I check if an app running on Android?

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
}

How to check if application is running on background?

try

/**Flag to determine if the Activity is visible*/
private static boolean activityVisible;
private static boolean activityDestroy;

/**Is the application actually visible on the mobile screen*/
public static boolean isActivityVisible(){
return activityVisible;
}

/**Is the application actually destroyed*/
public static boolean isActivityDestroy(){
return activityDestroy;
}

/**Is the application actually destroyed*/
public static void activityDestroy(boolean isDestroy){
activityDestroy = isDestroy;
}

/**Is the application actually in the background*/
public static boolean isActivityInBackground(){
return !activityVisible;
}


/**Change the state of the Application to resume*/
public static void activityResumed() {
activityVisible = true;
}

/**Change the state of the Application to paused*/
public static void activityPaused() {
activityVisible = false;
}

and then for checking

    Application app = ((Application)getApplicationContext());
boolean visible = app.isActivityVisible();

Android: How to check application is Running or not

There is no any way to get the all running apps in API level (>21) for the security reasons.

BUT

You can access to app usage history and statistics with the time
intervals: days, weeks, months, and years with UsageStatsManager

Here is official Docs Link

The Other Apps like clean master , ccleaner use this technic to get running apps.

Here is the Example to get apps list using UsageStatsManager

Note: You must give Usage Access Permission before use UsageStatsManager

How can I tell if Android app is running in the foreground?

Make a global variable like private boolean mIsInForegroundMode; and assign a false value in onPause() and a true value in onResume().

Sample code:

private boolean mIsInForegroundMode;

@Override
protected void onPause() {
super.onPause();
mIsInForegroundMode = false;
}

@Override
protected void onResume() {
super.onResume();
mIsInForegroundMode = true;
}

// Some function.
public boolean isInForeground() {
return mIsInForegroundMode;
}

How can i check if an app is open in Android?

You can get the running Application package name using this function:

public String foregroundPackage() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> runningTaskInfo = manager.getRunningTasks(1);
ComponentName componentInfo = runningTaskInfo.get(0).topActivity;
packageName = componentInfo.getPackageName();
return componentInfo.getPackageName();
}

Android for work - How to check if my application is running in the work profile?

I found a solution :
if isProfileOwnerApp return true for one package name, it means that your app (badged) is running on work profile.
if your app is running in normal mode (no badge) isProfileOwnerApp return false for all admins even if there is a Work profile.

DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
List<ComponentName> activeAdmins = devicePolicyManager.getActiveAdmins();
if (activeAdmins != null){
for (ComponentName admin : activeAdmins){
String packageName= admin.getPackageName();
Log.d(TAG, "admin:"+packageName);
Log.d(TAG, "profile:"+ devicePolicyManager.isProfileOwnerApp(packageName));
Log.d(TAG, "device:"+ devicePolicyManager.isDeviceOwnerApp(packageName));
}
}

How to check if a service is running on Android?

I had the same problem not long ago. Since my service was local, I ended up simply using a static field in the service class to toggle state, as described by hackbod here

EDIT (for the record):

Here is the solution proposed by hackbod:

If your client and server code is part of the same .apk and you are
binding to the service with a concrete Intent (one that specifies the
exact service class), then you can simply have your service set a
global variable when it is running that your client can check.

We deliberately don't have an API to check whether a service is
running because, nearly without fail, when you want to do something
like that you end up with race conditions in your code.



Related Topics



Leave a reply



Submit