Android系统为我们提供了很多服务管理的类,包括ActivityManager、PowerManager(电源管理)、AudioManager(音频管理)等。除此之外,还提供了一个PackageManger管理类,它的主要职责是管理应用程序包。 通过PackageManager,我们就可以获取应用程序信息。
提到PackageManager,就得提一下AndroidManifest.XML文件了。AndroidManifest.xml是Android应用程序中最重要的文件之一。它是Android程序的全局配置文件,是每个 android程序中必须的文件。它位于我们开发的应用程序的根目录下,描述了package中的全局数据,包括package中暴露的组件 (activities, services, 等等),以及他们各自的实现类,各种能被处理的数据和启动位置等重要信息。
因此,该文件提供了Android系统所需要的关于该应用程序的必要信息,即在该应用程序的任何代码运行之前系统所必须拥有的信息。PackageManager获取的信息即来自AndroidManifest.XML。
1、安装,卸载应用 2、查询permission相关信息 3、查询Application相关信息(application,activity,receiver,service,provider及相应属性等) 4、查询已安装应用 5、增加,删除permission 6、清除用户数据、缓存,代码段等
PackageManager packageManager = context.getPackageManager();
获取不同信息主要是传入不同的flag值
//第一个参数传入应用包名,
//第二个参数是一个flag表示你想要获取哪些信息,比如下例的activity信息及activity标签下的metadata信息
PackageInfo packageInfo = packageManager.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES | PackageManager.GET_META_DATA);
1、权限信息(AndroidManifest中<uses-permission/>标签中信息列表)
String[] permisions = packageManager.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS).requestedPermissions;
2、版本信息
PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);
String versionName = packageInfo.versionName;
int versionCode = packageInfo.versionCode;
3、其他信息
比如sharedUserId、packageName等等
//由上述packageInfo中获取得到
ApplicationInfo applicationInfo = packageInfo.applicationInfo;
1、应用名称
String appName = (String) applicationInfo.loadLabel(packageManager);
2、应用图标
Drawable appIcon = applicationInfo.loadIcon(packageManager);
3、其他信息
比如taskAffinity、processName、theme等
Activity或者BroadcastReceiver信息都是通过ActivityInfo获取的
//flag值必须包含PackageManager.GET_ACTIVITIES,如果还要获取其他信息如metadata则增加一个flag值PackageManager.GET_META_DATA
PackageInfo packageInfo = packageManager.getPackageInfo(packageName,PackageManager.GET_ACTIVITIES);
//activity信息
ActivityInfo[] activities = packageInfo.activities;
//flag值必须包含PackageManager.GET_RECEIVERS,如果还要获取其他信息如metadata则增加一个flag值PackageManager.GET_META_DATA
PackageInfo packageInfo = packageManager.getPackageInfo(packageName,PackageManager.GET_RECEIVERS);
//broadcastreceiver信息
ActivityInfo[] receivers = packageInfo.receivers
1、activity全路径
String activityClassName = activityInfo.name;
2、其他信息
比如theme、launchMode、taskAffinity等
Service信息通过ServiceInfo获取
//flag值必须包含PackageManager.GET_SERVICES,如果还要获取其他信息如metadata则增加一个flag值PackageManager.GET_META_DATA
PackageInfo packageInfo = packageManager.getPackageInfo(packageName,PackageManager.GET_SERVICES);
ServiceInfo[] services = packageInfo.services;
比如flags、permission
ContentProvider信息通过ProviderInfo获取
//flag值必须包含PackageManager.GET_PROVIDERS,如果还要获取其他信息如metadata则增加一个flag值PackageManager.GET_META_DATA
PackageInfo packageInfo = packageManager.getPackageInfo(packageName,PackageManager.GET_PROVIDERS);
ServiceInfo[] services = packageInfo.services;
比如authority、readPermission、writePermission等
//首先获取packageManager
PackageManager packageManager = context.getPackageManager();
//获取packageInfo信息
PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);
//获取应用版本号
int versionCode = packageInfo.versionCode;
//获取应用版本名
String versionName = packageInfo.versionName;
//获取应用名称
String appName = (String) packageInfo.applicationInfo.loadLabel(packageManager);
//获取应用图标
Drawable appIcon = packageInfo.applicationInfo.loadIcon(packageManager)
//<application/>标签下metadata获取
ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
Bundle metadata = applicationInfo.metaData;//然后通过改bundle获取相应值,用法见bundle
//<activity/>标签下metadata获取(其他service\receiver\provider同理,切换flag即可)
//入口信息获取
PackageInfo packageInfo = packageManager.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES | PackageManager.GET_META_DATA);
ActivityInfo[] activityInfos = packageInfo.activities;
for (ActivityInfo activityInfo : activityInfos) {
Bundle metadata = activityInfo.metaData;//然后通过改bundle获取相应值,用法
见bundle
String activityClassName = activityInfo.name;
}
PackageManager packageManager = context.getPackageManager();
//apkPath传入apk绝对路径
PackageInfo pInfo = packageManager.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES);
if (pInfo != null) {
//包名
String packageName = pInfo.packageName;
//版本名
String versionCode = pInfo.versionName;
//版本号
String versionCode = pInfo.versionCode;
ApplicationInfo appInfo = pInfo.applicationInfo;
/** 必须加这两句,不然下面icon获取是default icon而不是应用包的icon */
appInfo.sourceDir = apkPath;
appInfo.publicSourceDir = apkPath;
//应用名
String appName = appInfo.loadLabel(packageManager).toString();
//应用图标
Drawable appIcon = packageManager.getApplicationIcon(appInfo);
//...其他信息同上获取已安装应用信息一样
}
public static void openApplication(Context context, String packageName) throws Exception {
Intent launcherIntent = new Intent("android.intent.action.MAIN", null);
launcherIntent.addCategory("android.intent.category.LAUNCHER");
launcherIntent.setPackage(packageName);
ResolveInfo localResolveInfo = (ResolveInfo) context.getPackageManager()
.queryIntentActivities(launcherIntent, 0).iterator().next();
if (localResolveInfo != null) {
String str = localResolveInfo.activityInfo.name;
Intent targetIntent = new Intent(Intent.ACTION_MAIN);
targetIntent.addCategory(Intent.CATEGORY_LAUNCHER);
targetIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
targetIntent.setComponent(new ComponentName(packageName, str));
context.startActivity(localIntent2);
}
PackageManager packageManager = mContext.getPackageManager();
List<PackageInfo> mPacks = packageManager.getInstalledPackages(0);
for(PackageInfo packageInfo : mPacks) {
//是否是系统应用
boolean isUserApp = (packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0;
//...同上获取其他信息
}
packageManager.getPackageSizeInfo(packageName, new IPackageStatsObserver.Stub() {
@Override
public void onGetStatsCompleted(PackageStats packageStats, boolean b) throws RemoteException {
String package = packageStats.packageName;
long totalSize = packageStats.cacheSize + packageStats.codeSize + packageStats.dataSize;
});