【Android】判断某个AP是否在系统中存在(PackageManager与PackageInfo)

本文介绍如何使用Android SDK中的PackageManager类和PackageInfo类来判断特定应用是否已安装在系统中,并提供了一个实用的工具类实现。通过解析应用的Manifest.xml文件,可以获取到应用的详细信息,包括权限、组件等。最后,展示了一个简单的判断应用是否已安装的方法,适用于各种Android开发场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

public abstract class

PackageManager

extends Object
java.lang.Object
android.content.pm.PackageManager
Known Direct Subclasses
Class for retrieving various kinds of information related to the application packages that are currently installed on the device. You can find this class throughgetPackageManager().
【1】使用从Manifest.xml文件中获得到的PackageInfo来取得AP的信息.
public class

PackageInfo

extends Object
implements Parcelable
java.lang.Object
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 finalCreator<PackageInfo>CREATOR
Since: API Level 1

publicActivityInfo[]activities
Since: API Level 1

Array of all<activity>tags included under <application>, or null if there were none. This is only filled in if the flagGET_ACTIVITIESwas set.

publicApplicationInfoapplicationInfo
Since: API Level 1

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

publicConfigurationInfo[]configPreferences
Since: API Level 3

Application specified preferred configuration<uses-configuration>tags included under <manifest>, or null if there were none. This is only filled in if the flagGET_CONFIGURATIONSwas set.

public longfirstInstallTime
Since: API Level 9

The time at which the app was first installed. Units are as percurrentTimeMillis().

public int[]gids
Since: API Level 1

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

publicInstrumentationInfo[]instrumentation
Since: API Level 1

Array of all<instrumentation>tags included under <manifest>, or null if there were none. This is only filled in if the flagGET_INSTRUMENTATIONwas set.

public longlastUpdateTime
Since: API Level 9

The time at which the app was last updated. Units are as percurrentTimeMillis().

publicStringpackageName
Since: API Level 1

The name of this package. From the <manifest> tag's "name" attribute.

publicPermissionInfo[]permissions
Since: API Level 1

Array of all<permission>tags included under <manifest>, or null if there were none. This is only filled in if the flagGET_PERMISSIONSwas set.

publicProviderInfo[]providers
Since: API Level 1

Array of all<provider>tags included under <application>, or null if there were none. This is only filled in if the flagGET_PROVIDERSwas set.

publicActivityInfo[]receivers
Since: API Level 1

Array of all<receiver>tags included under <application>, or null if there were none. This is only filled in if the flagGET_RECEIVERSwas set.

publicFeatureInfo[]reqFeatures
Since: API Level 5

The features that this application has said it requires.

publicString[]requestedPermissions
Since: API Level 1

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

publicServiceInfo[]services
Since: API Level 1

Array of all<service>tags included under <application>, or null if there were none. This is only filled in if the flagGET_SERVICESwas set.

publicStringsharedUserId
Since: API Level 3

The shared user ID name of this package, as specified by the <manifest> tag'ssharedUserIdattribute.

public intsharedUserLabel
Since: API Level 3

The shared user ID label of this package, as specified by the <manifest> tag'ssharedUserLabelattribute.

publicSignature[]signatures
Since: API Level 1

Array of all signatures read from the package file. This is only filled in if the flagGET_SIGNATURESwas set.

public intversionCode
Since: API Level 1

The version number of this package, as specified by the <manifest> tag'sversionCodeattribute.

publicStringversionName
Since: API Level 1

The version name of this package, as specified by the <manifest> tag'sversionNameattribute.

【2】使用上面两个类来实现一个判断某个AP是否安装在系统中的工具类
	/**
	 * Whether the AP was installed
	 * @param context:the Context
	 * @param packageName:the ap packageName
	 * @return
	 */
	public static boolean isAppInstalled(Context context, String packageName)
	{
		//获取到一个PackageManager的instance
		final PackageManager packageManager = context.getPackageManager();
		//PERMISSION_GRANTED = 0
		List<PackageInfo> mPackageInfo = packageManager.getInstalledPackages(0);
		boolean flag = false;
		if(mPackageInfo != null)
		{
			String tempName = null;
			for(int i = 0; i < mPackageInfo.size(); i++)
			{
				//获取到AP包名
				tempName = mPackageInfo.get(i).packageName;
				if(tempName != null && tempName.equals(packageName))
				{
					MyLogger.kLog().i("Package[" + packageName + "]:is installed.");
					flag = true;
					break;
				}
			}
		}
		return flag;
	}


 
 
 
public abstractList<PackageInfo>getInstalledPackages(int flags)
Since: API Level 1

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

Parameters
flags Additional option flags. Use any combination ofGET_ACTIVITIES,GET_GIDS,GET_CONFIGURATIONS,GET_INSTRUMENTATION,GET_PERMISSIONS,GET_PROVIDERS,GET_RECEIVERS,GET_SERVICES,GET_SIGNATURES,GET_UNINSTALLED_PACKAGESto 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.


谢谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值