---
title: "【Android】判断某个AP是否在系统中存在（PackageManager与PackageInfo）"
author: "mmdev"
date: "2011-12-09T16:48:00Z"
tags: []
slug: "android-ap-packagemanager-packageinfo"
draft: false
lastmod: "2012-05-07T21:19:30Z"
id: 1515277
url: "https://mmdev.iteye.com/blog/1515277"
---

【0】我们可以使用`getPackageManager() 方法来查询安装在系统上的AP.`

public abstract class

# PackageManager

extends[Object](http://developer.android.com/reference/java/lang/Object.html)

[java.lang.Object](http://developer.android.com/reference/java/lang/Object.html) ↳ android.content.pm.PackageManager

[![](http://developer.android.com/assets/images/triangle-closed.png)](http://developer.android.com/reference/android/content/pm/PackageManager.html)Known Direct Subclasses

[MockPackageManager](http://developer.android.com/reference/android/test/mock/MockPackageManager.html)

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()`.

【1】使用从Manifest.xml文件中获得到的PackageInfo来取得AP的信息.

public class

# PackageInfo

extends[Object](http://developer.android.com/reference/java/lang/Object.html)  
implements[Parcelable](http://developer.android.com/reference/android/os/Parcelable.html)

[java.lang.Object](http://developer.android.com/reference/java/lang/Object.html) ↳ android.content.pm.PackageInfo

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

[]()

#### public static final[Creator](http://developer.android.com/reference/android/os/Parcelable.Creator.html)&lt;[PackageInfo](http://developer.android.com/reference/android/content/pm/PackageInfo.html)&gt;CREATOR

Since:[API Level 1](http://developer.android.com/guide/appendix/api-levels.html#level1)

[]()

#### public[ActivityInfo\[\]](http://developer.android.com/reference/android/content/pm/ActivityInfo.html)activities

Since:[API Level 1](http://developer.android.com/guide/appendix/api-levels.html#level1)

Array of all`<activity>`tags included under &lt;application&gt;, or null if there were none. This is only filled in if the flag`GET_ACTIVITIES`was set.

[]()

#### public[ApplicationInfo](http://developer.android.com/reference/android/content/pm/ApplicationInfo.html)applicationInfo

Since:[API Level 1](http://developer.android.com/guide/appendix/api-levels.html#level1)

Information collected from the &lt;application&gt; tag, or null if there was none.

[]()

#### public[ConfigurationInfo\[\]](http://developer.android.com/reference/android/content/pm/ConfigurationInfo.html)configPreferences

Since:[API Level 3](http://developer.android.com/guide/appendix/api-levels.html#level3)

Application specified preferred configuration`<uses-configuration>`tags included under &lt;manifest&gt;, or null if there were none. This is only filled in if the flag`GET_CONFIGURATIONS`was set.

[]()

#### public longfirstInstallTime

Since:[API Level 9](http://developer.android.com/guide/appendix/api-levels.html#level9)

The time at which the app was first installed. Units are as per`currentTimeMillis()`.

[]()

#### public int\[]gids

Since:[API Level 1](http://developer.android.com/guide/appendix/api-levels.html#level1)

All kernel group-IDs that have been assigned to this package. This is only filled in if the flag`GET_GIDS`was set.

[]()

#### public[InstrumentationInfo\[\]](http://developer.android.com/reference/android/content/pm/InstrumentationInfo.html)instrumentation

Since:[API Level 1](http://developer.android.com/guide/appendix/api-levels.html#level1)

Array of all`<instrumentation>`tags included under &lt;manifest&gt;, or null if there were none. This is only filled in if the flag`GET_INSTRUMENTATION`was set.

[]()

#### public longlastUpdateTime

Since:[API Level 9](http://developer.android.com/guide/appendix/api-levels.html#level9)

The time at which the app was last updated. Units are as per`currentTimeMillis()`.

[]()

#### public[String](http://developer.android.com/reference/java/lang/String.html)packageName

Since:[API Level 1](http://developer.android.com/guide/appendix/api-levels.html#level1)

The name of this package. From the &lt;manifest&gt; tag's "name" attribute.

[]()

#### public[PermissionInfo\[\]](http://developer.android.com/reference/android/content/pm/PermissionInfo.html)permissions

Since:[API Level 1](http://developer.android.com/guide/appendix/api-levels.html#level1)

Array of all`<permission>`tags included under &lt;manifest&gt;, or null if there were none. This is only filled in if the flag`GET_PERMISSIONS`was set.

[]()

#### public[ProviderInfo\[\]](http://developer.android.com/reference/android/content/pm/ProviderInfo.html)providers

Since:[API Level 1](http://developer.android.com/guide/appendix/api-levels.html#level1)

Array of all`<provider>`tags included under &lt;application&gt;, or null if there were none. This is only filled in if the flag`GET_PROVIDERS`was set.

[]()

#### public[ActivityInfo\[\]](http://developer.android.com/reference/android/content/pm/ActivityInfo.html)receivers

Since:[API Level 1](http://developer.android.com/guide/appendix/api-levels.html#level1)

Array of all`<receiver>`tags included under &lt;application&gt;, or null if there were none. This is only filled in if the flag`GET_RECEIVERS`was set.

[]()

#### public[FeatureInfo\[\]](http://developer.android.com/reference/android/content/pm/FeatureInfo.html)reqFeatures

Since:[API Level 5](http://developer.android.com/guide/appendix/api-levels.html#level5)

The features that this application has said it requires.

[]()

#### public[String\[\]](http://developer.android.com/reference/java/lang/String.html)requestedPermissions

Since:[API Level 1](http://developer.android.com/guide/appendix/api-levels.html#level1)

Array of all`<uses-permission>`tags included under &lt;manifest&gt;, or null if there were none. This is only filled in if the flag`GET_PERMISSIONS`was set. This list includes all permissions requested, even those that were not granted or known by the system at install time.

[]()

#### public[ServiceInfo\[\]](http://developer.android.com/reference/android/content/pm/ServiceInfo.html)services

Since:[API Level 1](http://developer.android.com/guide/appendix/api-levels.html#level1)

Array of all`<service>`tags included under &lt;application&gt;, or null if there were none. This is only filled in if the flag`GET_SERVICES`was set.

[]()

#### public[String](http://developer.android.com/reference/java/lang/String.html)sharedUserId

Since:[API Level 3](http://developer.android.com/guide/appendix/api-levels.html#level3)

The shared user ID name of this package, as specified by the &lt;manifest&gt; tag's`sharedUserId`attribute.

[]()

#### public intsharedUserLabel

Since:[API Level 3](http://developer.android.com/guide/appendix/api-levels.html#level3)

The shared user ID label of this package, as specified by the &lt;manifest&gt; tag's`sharedUserLabel`attribute.

[]()

#### public[Signature\[\]](http://developer.android.com/reference/android/content/pm/Signature.html)signatures

Since:[API Level 1](http://developer.android.com/guide/appendix/api-levels.html#level1)

Array of all signatures read from the package file. This is only filled in if the flag`GET_SIGNATURES`was set.

[]()

#### public intversionCode

Since:[API Level 1](http://developer.android.com/guide/appendix/api-levels.html#level1)

The version number of this package, as specified by the &lt;manifest&gt; tag's`versionCode`attribute.

[]()

#### public[String](http://developer.android.com/reference/java/lang/String.html)versionName

Since:[API Level 1](http://developer.android.com/guide/appendix/api-levels.html#level1)

The version name of this package, as specified by the &lt;manifest&gt; tag's`versionName`attribute.

【2】使用上面两个类来实现一个判断某个AP是否安装在系统中的工具类

```

```

```

```

#### public abstract[List](http://developer.android.com/reference/java/util/List.html)&lt;[PackageInfo](http://developer.android.com/reference/android/content/pm/PackageInfo.html)&gt;getInstalledPackages(int flags)

Since:[API Level 1](http://developer.android.com/guide/appendix/api-levels.html#level1)

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

##### Parameters

flags Additional option flags. Use any combination of`GET_ACTIVITIES`,`GET_GIDS`,`GET_CONFIGURATIONS`,`GET_INSTRUMENTATION`,`GET_PERMISSIONS`,`GET_PROVIDERS`,`GET_RECEIVERS`,`GET_SERVICES`,`GET_SIGNATURES`,`GET_UNINSTALLED_PACKAGES`to modify the data returned.

##### Returns

- A List of PackageInfo objects, one for each package that is installed on the device. In the unlikely case of there being no installed packages, an empty list is returned. If flag GET\_UNINSTALLED\_PACKAGES is set, a list of all applications including those deleted with DONT\_DELETE\_DATA (partially installed apps with data directory) will be returned.

谢谢！