Android development implements the method of judging the running state of APP according to the package name

This paper describes the method of Android development and implementation to judge the running state of APP according to the package name. Share with you for your reference, as follows:

Previously, we talked about the method of Android development to judge whether an app application is running. Here, we realize 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 handled according to whether there is a service in the app:

① No service

② Have service

For apps without service, once the program is switched to the background, it may be recycled soon. Here, use the activitymanager.getrunningtasks (int maxnum) method to obtain the currently running tasks. Note: this method is not recommended by the system and is a deprecated method.

For apps with services, most of them will have multiple services, and they may all be of remote type. Therefore, certain processing needs to be carried out in judgment. Here, it is judged according to the uid of the app to avoid inaccurate judgment of survival in some special cases. We use the activitymanager. Getrunningservices (int maxnum) method to get the list of currently running services.

Note: the uid of the app is not unique to the built-in app of the system. The built-in app of Android will share the uid. If you are developing built-in applications or similar things, be sure to check them in a special way.

realization

The following are several tool classes that need to be used together in order to cover all situations:

In formal use, the two can be combined:

summary

In the process of exploring the survival of the verifier, I found that activitymanager.runningserviceinfo contains a lot of information. At first, I used its process and started attributes to judge. Process corresponds to the package name, but it can't be judged when there is only a remote type service.

More readers interested in Android related content can view the special topics of this site: introduction and advanced tutorial of Android development, summary of Android debugging skills and common problem solving methods, summary of Android basic component usage, summary of Android view skills, summary of Android layout skills and summary of Android control usage

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>