获取已经安装应用程序的相关信息 PackageManaer

做Android开发也已经很久了,但发现到现在为止,需要什么功能的时候,仍然需要查来查去,即使是之前做过的也是这样,所以干脆在blog里面加个分类,记录一下自己用过的内容。

本文章主要是记录一下,怎么去获取已经安装的应用程序的各种信息。

首先先定位到相关的API,在Android的Reference中,我们可以看到官方是这样对PackageManager进行介绍的

Class for retrieving various kinds of information related to the application packages that are currently installed on the device. You can find this class through getPackageManager().

由此可见,该类是用来获取在设备上的安装的程序的各种信息,使用Context类中的 getPackageManager()可以获取类的一个实例。看看下面都有什么成员变量和方法,在成员变量中,我们注意到有这些:

int	GET_ACTIVITIES	PackageInfo flag: return information about activities in the package in activities.
int	GET_CONFIGURATIONS	PackageInfo flag: return information about hardware preferences in PackageInfo.configPreferences and requested features in PackageInfo.reqFeatures.
int	GET_DISABLED_COMPONENTS	PackageInfo flag: include disabled components in the returned info.
int	GET_GIDS	PackageInfo flag: return the group ids that are associated with an application.
int	GET_INSTRUMENTATION	PackageInfo flag: return information about instrumentation in the package in instrumentation.
int	GET_INTENT_FILTERS	PackageInfo flag: return information about the intent filters supported by the activity.
int	GET_META_DATA	ComponentInfo flag: return the metaData data Bundles that are associated with a component.
int	GET_PERMISSIONS	PackageInfo flag: return information about permissions in the package in permissions.
int	GET_PROVIDERS	PackageInfo flag: return information about content providers in the package in providers.
int	GET_RECEIVERS	PackageInfo flag: return information about intent receivers in the package in receivers.
int	GET_RESOLVED_FILTER	ResolveInfo flag: return the IntentFilter that was matched for a particular ResolveInfo in filter.
int	GET_SERVICES	PackageInfo flag: return information about services in the package in services.
int	GET_SHARED_LIBRARY_FILES	ApplicationInfo flag: return the paths to the shared libraries that are associated with an application.
int	GET_SIGNATURES	PackageInfo flag: return information about the signatures included in the package.
int	GET_UNINSTALLED_PACKAGES	Flag parameter to retrieve some information about all applications (even uninstalled ones) which have data directories.
int	GET_URI_PERMISSION_PATTERNS	ProviderInfo flag: return the URI permission patterns that are associated with a content provider.

似乎接近了,里面有GET_ACTIVITIES,GET_INTENT_FILTERS等等,都是我们很熟悉的,猜想便是用这些来作为标志,获取不同的信息,再看看方法。

abstract Drawable	 getActivityIcon(Intent intent)
Retrieve the icon associated with an Intent.
abstract Drawable	 getActivityIcon(ComponentName activityName)
Retrieve the icon associated with an activity.
abstract ActivityInfo	 getActivityInfo(ComponentName component, int flags)
Retrieve all of the information we know about a particular activity class.
abstract Drawable	 getActivityLogo(Intent intent)
Retrieve the logo associated with an Intent.
abstract Drawable	 getActivityLogo(ComponentName activityName)
Retrieve the logo associated with an activity.
abstract List<PermissionGroupInfo>	 getAllPermissionGroups(int flags)
Retrieve all of the known permission groups in the system.
abstract int	 getApplicationEnabledSetting(String packageName)
Return the the enabled setting for an application.
abstract Drawable	 getApplicationIcon(String packageName)
Retrieve the icon associated with an application.
abstract Drawable	 getApplicationIcon(ApplicationInfo info)
Retrieve the icon associated with an application.
abstract ApplicationInfo	 getApplicationInfo(String packageName, int flags)
Retrieve all of the information we know about a particular package/application.
abstract CharSequence	 getApplicationLabel(ApplicationInfo info)
Return the label to use for this application.
abstract Drawable	 getApplicationLogo(String packageName)
Retrieve the logo associated with an application.
abstract Drawable	 getApplicationLogo(ApplicationInfo info)
Retrieve the logo associated with an application.
abstract int	 getComponentEnabledSetting(ComponentName componentName)
Return the the enabled setting for a package component (activity, receiver, service, provider).
abstract Drawable	 getDefaultActivityIcon()
Return the generic icon for an activity that is used when no specific icon is defined.
abstract Drawable	 getDrawable(String packageName, int resid, ApplicationInfo appInfo)
Retrieve an image from a package.
abstract List<ApplicationInfo>	 getInstalledApplications(int flags)
Return a List of all application packages that are installed on the device.
abstract List<PackageInfo>	 getInstalledPackages(int flags)
Return a List of all packages that are installed on the device.

看到里面那些以get开头的函数,注意到里面有两个返回值为List<T>的函数,

abstract List<ApplicationInfo> getInstalledApplications(int flags)

Return a List of all application packages that are installed on the device.
abstract List<PackageInfo> getInstalledPackages(int flags)

Return a List of all packages that are installed on the device.

貌似这两个函数都能满足我们的需求,那么他们有什么不同呢?其介绍,一个返回的all application packages,一个返回的all packages,看不出什么区别,我们再看看返回值ApplicationInfo和PackageInfo的区别吧:

ApplicationInfo:Information you can retrieve about a particular application. This corresponds to information collected from the AndroidManifest.xml’s <application> tag.

PackageInfo:

Overall information about the contents of a package. This corresponds to all of the information collected from AndroidManifest.xml.

发现了,原来都是从AndroidManifest.xml中读取的,但一个读的是application标签,一个全部读取。进一步查看其成员变量等,发现PackageInfo要比Application信息要全一下,好了,那明白区别之后,我们就开始写吧,这里用的是PackageInfo。

首先我们需要获取PackageInfo的List

List<PackageInfo> listPackageInfos=context.getPackageManager().getInstalledPackages(PackageManager.GET_ACTIVITIES);

这样我们就拿到了一个包含所有安装程序信息的List,这个工作耗时比较长,因此在实际使用的时候,要新开一个线程,否则会将主线程阻塞。然后我们看看PackageInfo都有什么?

public ApplicationInfo applicationInfo Information collected from the <application> tag, or null if there was none.

(从这里可以看出,确实从getInstalledPackages函数读到的信息更全面一写)

public String packageName The name of this package.
public int versionCode The version number of this package, as specified by the <manifest> tag’s versionCode attribute.
public String versionName The version name of this package, as specified by the <manifest> tag’sversionName attribute.

我大体列了几个,但发现里面没有Application的图标啥的,从哪找?我们去看看ApplicationInfo里面有什么?

void dumpBack(Printer pw, String prefix)
void dumpFront(Printer pw, String prefix)
Drawable loadIcon(PackageManager pm)
Retrieve the current graphical icon associated with this item.

CharSequenceloadLabel(PackageManager pm)

Retrieve the current textual label associated with this item.

DrawableloadLogo(PackageManager pm)

Retrieve the current graphical logo associated with this item.

XmlResourceParserloadXmlMetaData(PackageManager pm, String name)

Load an XML resource attached to the meta-data of this item.

voidwriteToParcel(Parcel dest, int parcelableFlags)看到了吧,其从 From class android.content.pm.PackageItemInfo继承过来了这些函数,我们就可以读到图标的信息,好了,万事具备了,开工!

	public ArrayList<PackManagerHelperApplicationInfo> getUserApplicationInfos(){
		ArrayList<PackManagerHelperApplicationInfo> applicationInfos=new ArrayList<PackManagerHelperApplicationInfo>();
		List<PackageInfo> listPackageInfos=context.getPackageManager().getInstalledPackages(PackageManager.GET_ACTIVITIES);

		for(int i=0;i<listPackageInfos.size();i++)
		{
			if((listPackageInfos.get(i).applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)!=0)
			{
				continue;
			}
			PackManagerHelperApplicationInfo tmpApplicationInfo=new PackManagerHelperApplicationInfo();
			tmpApplicationInfo.appName=listPackageInfos.get(i).applicationInfo.loadLabel(context.getPackageManager()).toString();
			tmpApplicationInfo.packageName=listPackageInfos.get(i).packageName;
			tmpApplicationInfo.versionName=listPackageInfos.get(i).versionName;
			tmpApplicationInfo.versionCode=listPackageInfos.get(i).versionCode;
			tmpApplicationInfo.appIcon=listPackageInfos.get(i).applicationInfo.loadIcon(context.getPackageManager());

			applicationInfos.add(tmpApplicationInfo);			
		}

		return applicationInfos;		
	}

大家可能注意到,

if((listPackageInfos.get(i).applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)!=0

这句子用来干嘛呢?是用来去掉系统自带的程序的,这个有很多flag,不一一尝试了。

上面这样一折腾,就搞明白了怎么读取信息,也蛮简单。

后记:还有一个小细节,使用

public abstract Intent getLaunchIntentForPackage (String packageName)

可以获取到Launcher中启动的action。

 

About: happyhls


发表评论

电子邮件地址不会被公开。 必填项已用*标注