Android judges the running status of the App based on the package name

Obtain whether the APP with the specified package name is still running in the background, and judge whether the APP is alive.

background

According to whether there may App  Service divided into two categories circumstances:

  • No Service
  • Have Service

For Service apps that don't have it  , once the program is switched to the background, it may be recycled soon. Here we use the  ActivityManager.getRunningTasks(int maxNum) method to get the currently running tasks. Note: This method is not recommended by the system and is a  Deprecated method.

For  Service the App, there will be more than most  Service , and possibly all  :remote types, so that the judgment requires some treatment, according to App is here  uid to judge, to avoid in some special cases to determine survival is not accurate. We use the  ActivityManager.getRunningServices(int maxNum) method to get the currently running  Service list.

Note: App is  uid not unique to the built-in apps uid in the system. Android built-in apps will share some common  situations. If you are developing system built-in applications or similar things, be sure to check special methods yourself.

achieve

The following are several tool classes, which need to be used together to cover all situations when they are officially used:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
    * Method description: Determine whether an application is running
    * Created by cafeting on 2017/2/4.
    * @param context context
    * @param packageName The package name of the application
    * @return true means running, false means not running
    */
   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;
   }


//Get the uid of the installed application, -1 means the application is not installed or the program is abnormal
   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;
   }

   /**
    * Determine whether a program of a uid has a running process, that is, whether it is alive
    * Created by cafeting on 2017/2/4.
    *
    * @param context context
    * @param uid The uid of the installed application
    * @return true means running, false means not running
    */
   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 the formal use, the two can be combined:

1
2
3
4
5
6
7
8
9
10
11
12
13
String pName = "xxx";
int uid = getPackageUid(context, pName);
if(uid > 0){
  	boolean rstA = isAppRunning(context, pName);
  	boolean rstB = isProcessRunning(context, uid);
  	if(rstA||rstB){
      	//The program with the specified package name is running
  	}else{
      	//The program of the specified package name is not running
  	}
}else{
  	//The application is not installed
}

to sum up

In the process of exploring the survival of the verification program, I found that it  ActivityManager.RunningServiceInfo contains a lot of information. At first, I used its  process sum  started attribute to judge, which  process corresponds to the package name, but  when there is only  :remote type  service, I can’t judge it.

Guess you like

Origin blog.csdn.net/THMAIL/article/details/112239165