android determines whether the application has started an instance

  • 2021-11-29 08:21:28
  • OfStack

I will not talk too much nonsense, or just look at the code!


 /**
 *  Determine whether the application has started 
 *
 * @param context    Context object 
 * @param packageName  To determine the package name of the application 
 * @return boolean
 */
public static boolean isAppAlive(Context context, String packageName) {
  ActivityManager activityManager =
      (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
  List<ActivityManager.RunningAppProcessInfo> processInfos
      = activityManager.getRunningAppProcesses();
  for (int i = 0; i < processInfos.size(); i++) {
    if (processInfos.get(i).processName.equals(packageName)) {
        return true;
    }
  }

  return false;
}

Supplementary knowledge: android judges whether the current application is started, app is running, and whether an interface is sorted out in the foreground tool class

Let's look at the code directly!


/**
 *  Judge whether the current application is started or not 
 *
 * @param context
 * @return
 */
public static boolean getCurrentTask(Context context) {
	ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
	// Gets all current surviving task Information of 
	List<RunningTaskInfo> appProcessInfos = activityManager.getRunningTasks(Integer.MAX_VALUE);
	// Traversal, if task Adj. name And the current task Adj. name Is the same, return the true Otherwise, return false
	for (RunningTaskInfo process : appProcessInfos) {
		if (process.baseActivity.getPackageName().equals(context.getPackageName())
				|| process.topActivity.getPackageName().equals(context.getPackageName())) {
			return true;
		}
	}
	return false;
}
 
/**
 *  Return app Running status 
 *
 * @param context 1 A context
 * @return int 1: Front desk  2: Backstage  0: Nonexistent 
 */
public static int isAppAlive(Context context) {
	String packageName = PackageUtil.getAppProcessName(context);
	ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
	List<ActivityManager.RunningTaskInfo> listInfos = activityManager.getRunningTasks(20);
	//  Determine whether the program is at the top of the stack 
	if (listInfos.get(0).topActivity.getPackageName().equals(packageName)) {
		return 1;
	} else {
		//  Determine whether the program is in the stack 
		for (ActivityManager.RunningTaskInfo info : listInfos) {
			if (info.topActivity.getPackageName().equals(packageName)) {
				return 2;
			}
		}
		return 0;//  Can't be found in the stack, return 0
	}
}
 
 
/**
 *  Determine whether an interface is in the foreground 
 *
 * @param context
 * @param className  An interface name 
 */
public static boolean isActivityForeground(Context context, String className) {
	if (context == null || TextUtils.isEmpty(className)) {
		return false;
	}
	ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
	List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(1);
	if (list != null && list.size() > 0) {
		ComponentName cpn = list.get(0).topActivity;
		if (className.equals(cpn.getClassName())) {
			return true;
		}
	}
	return false;
}

Related articles: