1. Introduction Let's get straight to the point. In some scenarios, we need to get some information about other apps, such as app name, package name, icon, etc. At this time, we need to use the PackageManager class. This article will give a detailed explanation of the use of PackageManager to help you get the information of various apps, including the information of some apps that are not installed. 2. Some categories that need to be understood in advance When you need to obtain various information about a specified App, you need to operate some corresponding APIs provided by Android. You first need to obtain the PackageManager (hereinafter referred to as PM) object. Through the PM object, you can obtain the information class of each App you need. The App information classes involved here include: PackageInfo, ApplicationInfo, ActivityInfo/ServiceInfo/ProviderInfo, etc. There is also a ResolveInfo class, which is a bit special and is not subordinate to the previous structure. These classes can be divided according to the components defined in AndroidManifest.xml. The approximate structure is as follows. As you can see, the relationship between them is quite complicated. A PackageInfo corresponds to an ApplicationInfo, which in turn contains several ActivityInfo, ServiceInfo, and ProviderInfo. 2.1 PackageManager PackageManager is an abstract class. The PackageManager we generally operate is actually its implementation class ApplicationPackageManager object. In Context, there is a method to obtain the PM object, getPackageManager(), so it is very simple for the four major components to obtain it. PM provides a lot of methods for us to obtain PackageInfo objects, ApplicationInfo objects, etc. through different conditions. It is the basis of this article. 2.2 PackageInfo PackageInfo As the name suggests, it is mainly used to store some information of the obtained Package, including: package name (packageName), version number (versionCode), and version name (versionName). Basically, once you get PackageInfo, you can get most of the information related to this Apk. In addition, there is an applicationInfo field in PackageInfo, which can directly obtain the related ApplicationInfo object. Here are some commonly used fields in PackageInfo:
2.3 ApplicationInfo ApplicationInfo is less used than PackageInfo. It is mainly used to obtain some information defined in AndroidManifest.xml of the Apk. Here are a few commonly used price comparisons:
You can see that the information covered by ApplicationInfo is basically the information defined in AndroidManifest.xml, and some attributes are restricted by API Level. So if you are not sure about the attributes, check the documentation in advance to confirm whether all versions support them. 2.4 ActivityInfo ActivityInfo, ServiceInfo, and ProviderInfo are at the same level. You can tell them apart at a glance. They are the four major components defined by Android. Each covers a part of the information. Generally, when you obtain information from other apps externally, you will not get such detailed data. If you do, you can check these classes. Then I won’t waste time introducing them. 3. Basic Operations By now, you have basically understood the various categories of obtaining Apk information on the current device. It's a bit boring to just show the source code and talk about the properties. Next, let's take a look at how to use PM in detail with practical problems. 3.1 Get all installed apps If you want to get all the apps installed on the current device, you can use the getInstalledPackages() method to get the PackageInfo of all its installed apps. In PackageManager, many methods need to pass a flags field, which indicates the information of the app you need to obtain. There are many value ranges, and different flags are used to obtain different information. Usually, if there is no additional requirement, you can directly use GET_ACTIVITYS. 3.2 Determine whether the App is installed The main point here is to determine whether the App is installed on the current device by the package name. The simplest logic is to get PackageInfo. If you can get the data back, it means it is installed. 3.3 Get PackageInfo by package name In fact, when judging whether the App is installed, the PackageInfo information has already been obtained. Here we only need to return it. 3.4 Get the version number and version name Here you just need to get the corresponding value according to the fields in PackageInfo. 3.5 Get the name of the App To get the name of the App, you need to use the ApplicationInfo object, where the loadLabel() method returns the name of the App. 3.6 Get the App Icon In ApplicationInfo, you can also get the App Icon through loadIcon(), which returns a Drawable object that can be used directly. 3.7 Get PackageInfo according to the Apk file The methods described above are all based on the package name of an installed App to obtain additional information. But sometimes, we only have an uninstalled Apk file, and want to parse out the extra information in the Apk file. PM also has the corresponding API. It is very convenient, just use getPackageArchiveInfo(). As long as you get the PackageInfo information related to this Apk file, you can get other information such as name, icon, version number, version name, package name, etc. Similar to the examples introduced above, I will not list them one by one here. 4. Check for deficiencies and fill in the gaps In general, we have explained here how to use PM to obtain basic information about the App. However, it does not include some memory situations of the App when it is running. What data you want to obtain cannot be obtained by relying solely on PackageManager, so I will not elaborate on it here and will explain it in detail when I have the chance. In addition, many methods in PM may trigger the RuntimeException "Package manager has died", which is actually caused by frequent calls from many threads. If you want to avoid it, just lock and cache it. For more details, you can read my previous article "Can PackageManager be used indiscriminately?" [This article is an original article by 51CTO columnist "Zhang Yang". Please contact the author through WeChat official account to obtain authorization for reprinting] Click here to read more articles by this author |
<<: Want to quickly acquire customers? Here are 2 ways to promote your app to others
>>: Hundreds of millions of video views per day, Miaopai playback link optimization practice
The weather is getting cooler, and it is a good t...
As netizens' reading habits become increasing...
In my opinion, all businesses in China can be rou...
199IT original compilation When it comes to energ...
Recently I received letters from some netizens, s...
In this world, there are some brand names that, a...
" April 26, 2022 is the 15th National Malari...
Here I would like to share with you a review of a...
Mention Yangzhou Some people think of fresh and p...
Wuzhizhou Island is located on the coast of the S...
In the first week of September 2023, there were 1...
Author: Fang Yangxuan, Zhao Jingyuan Review | Zou...
Weibo may not bring direct sales, but it can subt...
The Sun is the only star in our solar system, but...
To be honest, as a veteran in the marketing indus...