Android development method to determine whether an app application is running

Android development implements the method of judging the running status of the App based on the package name | Service

As mentioned above, the method of Android development to determine whether an app is running , here is to obtain whether the app with the specified package name is still running in the background, and judge whether the app is alive .

background

It can be divided into two types according to whether the App has a Service:

① No Service

② There is Service

For Apps without Service, once the program is switched to the background, it may be recycled soon. Here, the  ActivityManager.getRunningTasks(int maxNum) method is used to obtain the currently running tasks. Note: This method is not recommended by the system, and it is a Deprecated method.

For Apps with Service, most of them will have multiple Services, and they may all be : remote type, so certain processing needs to be done in the judgment. Here, the judgment is based on the uid of the App, so as to avoid judging the survival in some special cases Inaccurate question. We use  ActivityManager.getRunningServices(int maxNum) the method to get the list of currently running Services .

Note: The app's uid is not unique to the system's built-in apps, and Android's built-in apps may share the uid. If you're developing an in-system app or something like that, be sure to do your own special way checks.

accomplish

The following are several tool classes, which need to be used in conjunction with official use to cover all situations:

/**
* 方法描述:判断某一应用是否正在运行
* Created by cafeting on 2017/2/4.
* @param context   上下文
* @param packageName 应用的包名
* @return true 表示正在运行,false 表示没有运行
*/
public static boolean isAppRunning(Context context, String packageName) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(100);
    if (list.size() <= 0) {
      return false;
    }
    for (ActivityManager.RunningTaskInfo info : list) {
      if (info.baseActivity.getPackageName().equals(packageName)) {
        return true;
      }
    }
    return false;
}
//获取已安装应用的 uid,-1 表示未安装此应用或程序异常
public static int getPackageUid(Context context, String packageName) {
    try {
      ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(packageName, 0);
      if (applicationInfo != null) {
        Logger.d(applicationInfo.uid);
        return applicationInfo.uid;
      }
    } catch (Exception e) {
      return -1;
    }
    return -1;
}
/**
* 判断某一 uid 的程序是否有正在运行的进程,即是否存活
* Created by cafeting on 2017/2/4.
*
* @param context   上下文
* @param uid 已安装应用的 uid
* @return true 表示正在运行,false 表示没有运行
*/
public static boolean isProcessRunning(Context context, int uid) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningServiceInfo> runningServiceInfos = am.getRunningServices(200);
    if (runningServiceInfos.size() > 0) {
      for (ActivityManager.RunningServiceInfo appProcess : runningServiceInfos){
        if (uid == appProcess.uid) {
          return true;
        }
      }
    }
    return false;
}

In official use, you can combine the two:

String pName = "xxx";
int uid = getPackageUid(context, pName);
if(uid > 0){
  boolean rstA = isAppRunning(context, pName);
  boolean rstB = isProcessRunning(context, uid);
  if(rstA||rstB){
    //指定包名的程序正在运行中
  }else{
    //指定包名的程序未在运行中
  }
}else{
  //应用未安装
}

Summarize

In the process of exploring and verifying the survival of the program, I found that  ActivityManager.RunningServiceInfo it contains a lot of information. At the beginning, I used its  process  and  started  attributes to judge, where process corresponds to the package name, but for only :remote type service , it cannot be judged.

Readers who are interested in more Android-related content can check out the topics on this site: " Introduction and Advanced Tutorials for Android Development ", "Summary of Android Debugging Skills and Common Problem Solving Methods", " Summary of Usage of Android Basic Components ", " Android View View Summary of Skills ", " Summary of Android Layout Layout Skills" and "Summary of Usage of Android Controls"

I hope that this article will be helpful to everyone's Android programming.

Guess you like

Origin blog.csdn.net/weixin_42602900/article/details/130196080