Android获取应用信息(AndroidManifest)

主要利用两个信息类:PackageInfo、ApplicationInfo

一、获取应用PackageInfo信息

以下packageManager都用这个表示

PackageManager packageManager = context.getPackageManager();

获取方式

==获取不同信息主要是传入不同的flag值==

//第一个参数传入应用包名,
//第二个参数是一个flag表示你想要获取哪些信息,比如下例的activity信息及activity标签下的metadata信息
PackageInfo packageInfo = packageManager.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES | PackageManager.GET_META_DATA);

具体信息获取

1、权限信息(AndroidManifest中标签中信息列表)

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等等

二、获取ApplicationInfo信息

获取方式

//由上述packageInfo中获取得到
ApplicationInfo applicationInfo = packageInfo.applicationInfo;

具体信息获取

1、应用名称

String appName = (String) applicationInfo.loadLabel(packageManager);

2、应用图标

Drawable appIcon = applicationInfo.loadIcon(packageManager);

3、其他信息

比如taskAffinity、processName、theme等

三、获取组件信息(ActivityInfo、ServiceInfo、ProviderInfo)

ActvitityInfo

==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等

ServiceInfo

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

ProviderInfo

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等

四、使用实例

1、根据包名获取应用信息
//首先获取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)

//标签下metadata获取
ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
Bundle metadata = applicationInfo.metaData;//然后通过改bundle获取相应值,用法见bundle

//标签下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;
}

2、扩展(直接获取未安装apk信息)
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);
    //...其他信息同上获取已安装应用信息一样
}
3、根据包名打开应用LauncherActivity
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);
}
4、获取所有已安装应用信息
PackageManager packageManager = mContext.getPackageManager();
List mPacks = packageManager.getInstalledPackages(0);
for(PackageInfo packageInfo : mPacks) {
    //是否是系统应用
    boolean isUserApp = (packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0;
    //...同上获取其他信息
}
5、获取应用大小数据
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;
});

你可能感兴趣的:(Android获取应用信息(AndroidManifest))