This article describes the method of Android development to determine the operating status of the App based on the package name. Share it for your reference, as follows:
As mentioned earlierHow to determine whether an app is running on Android, here I implement the APP with the specified package name to obtain whether the APP is still running in the background and determine whether the APP is alive.
background
It can be processed in two categories according to whether the App has Service:
① No Service
② There is a Service
For apps without Service, once the program switches to the background, it may be recycled soon. This is used here.(int maxNum)
Method to obtain the currently running task. Note: This method is not recommended by the system and is a Deprecated method.
For apps with Service, most of them will have multiple services, and they may be of type :remote, so a certain amount of processing is required in the judgment. Here we judge based on the app's uid to avoid the problem of inaccurate survival in some special circumstances. We use(int maxNum)
Method gets the currently running Service list.
Note: The uid of the App is not unique to the system's built-in apps, and Android's built-in applications will share the uid. If you are developing built-in applications or something similar, be sure to check them in special ways.
accomplish
The following are several tool classes that need to be used in conjunction with the official use to cover all situations:
/** * Method Description: Determine whether an application is running * Created by cafeting on 2017/2/4. * @param context * @param packageName application package name * @return true means running, false means not running */ public static boolean isAppRunning(Context context, String packageName) { ActivityManager am = (ActivityManager) (Context.ACTIVITY_SERVICE); List<> list = (100); if (() <= 0) { return false; } for ( info : list) { if (().equals(packageName)) { return true; } } return false; } //Get the uid of the installed application, -1 means that the application or program is not installed exceptionpublic static int getPackageUid(Context context, String packageName) { try { ApplicationInfo applicationInfo = ().getApplicationInfo(packageName, 0); if (applicationInfo != null) { (); return ; } } catch (Exception e) { return -1; } return -1; } /** * Determine whether a certain uid program has a running process, that is, whether it is alive * Created by cafeting on 2017/2/4. * * @param 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.ACTIVITY_SERVICE); List<> runningServiceInfos = (200); if (() > 0) { for ( appProcess : runningServiceInfos){ if (uid == ) { return true; } } } return false; }
When using it formally, you can combine both:
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 with the specified package name is not running } }else{ //The application is not installed}
Summarize
In the process of exploring the survival of the verification program, it was found thatIncludes a lot of information, I used it at the beginningprocess andstarted For attributes, process corresponds to package name, but cannot be determined when there is only :remote type service.
For more information about Android related content, please check out the topic of this site:Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.