Android应用程序管理系列(四)—— packagemanager flags 与 packageInfo 

Android PackageInfo 应用程序信息

1、简述

1 官方解析为  
    Overall information about the contents of a package.  This corresponds to all of the information collected from AndroidManifest.xml.
    获取应用程序所有的包信息,也包括从清单文件AndroidManifest.xml中获取的信息
2、获取PackageInfo
2.1 、获取手机上安装所有程序对应的 PackageInfo
//参数 0 是过滤信息 传入不同的数值,会获取不同的 应用程序信息 下面会具体分析 
List<PackageInfo> installedPackageList = packageManager.getInstalledPackages(0);
2.2 、获取指定应用程序对应的 PackageInfo

//参数 appPackageName 是对应应用程序的 包名 可通过 ApplicationInfo来获取
PackageInfo packageInfo = packageManager.getPackageInfo(appPackageName, 0);


Android PackageManager Flags 包管理者信息说明

1、简述
1、获取包管理者 
    PackageManager packageManager = this.getPackageManager();
2、可以通过包管理者来获取packageInfo
    //获取手机上安装的所有应用程序的包信息
    List<PackageInfo> installedPackageList = packageManager.getInstalledPackages(0);

    //获取指定包名应用程序的包信息
    PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);

3、上述在获取 PackageInfo时传入参数为 0,就是过滤条件
   在PackageManager中定义了多个过滤条件 ,传入的过滤条件不同,获取到的应用程序信息不同,
   例如上术传入的 过滤条件为 0 ,就是获取的 PackageInfo中 只包含 应用程序的版本信息(VersionCode,versionName)
2、PackageManager Flags概述
2.1 PackageManager.GET_ACTIVITIES

//源码解析 
 /**
  * {@link PackageInfo} flag: return information about
  * activities in the package in {@link PackageInfo#activities}.
  */
 public static final int GET_ACTIVITIES              = 0x00000001;

也就是当在获取PackageInfo时,传入的过滤条件为 PackageManager.GET_ACTIVITIES,那么我们获取的PackageInfo中就包含着 application标签下的所有的activity信息

//获取指定程序的application标签下的所有的activity
PackageInfo packageInfo = packageManager.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);


ActivityInfo[] activities = packageInfo.activities;

if (activities != null && activities.length != 0) {
     for (int i = 0; i < activities.length; i++) {
          Log.d("packageInfo -activities", "| " + activities[i]);
     }
}
2.2 PackageManager.GET_CONFIGURATIONS


    /**
     * {@link PackageInfo} flag: return information about
     * hardware preferences in
     * {@link PackageInfo#configPreferences PackageInfo.configPreferences},
     * and requested features in {@link PackageInfo#reqFeatures} and
     * {@link PackageInfo#featureGroups}.
     */
    public static final int GET_CONFIGURATIONS = 0x00004000;

也就是当在获取PackageInfo时,传入的过滤条件为 PackageManager.GET_CONFIGURATIONS,那么我们获取清单文件中所所有的configurations 信息


List<PackageInfo> list6 = packageManager.getInstalledPackages(PackageManager.GET_CONFIGURATIONS);


for (PackageInfo packageInfo : list6) {
     // 获取 清单文件中的所有的  Configuration 配制信息 
     ConfigurationInfo[] configPreferences = packageInfo.configPreferences;
     if (configPreferences != null) {
          for (int i = 0; i < configPreferences.length; i++) {

               ConfigurationInfo configPreference = configPreferences[i];
                Log.i("ConfigurationInfo-", "ConfigurationInfo: " + configPreference);
          }
     }
}
2.3 PackageManager.GET_CONFIGURATIONS


    /**
     * {@link PackageInfo} flag: return the
     * {@link PackageInfo#gids group ids} that are associated with an
     * application.
     * This applies for any API returning a PackageInfo class, either
     * directly or nested inside of another.
     */
    public static final int GET_GIDS                    = 0x00000100;

也就是当在获取PackageInfo时,传入的过滤条件为 PackageManager.GET_GIDS,那么我们获取到应用程序所有的 gid信息


List<PackageInfo> list7 = packageManager.getInstalledPackages(PackageManager.GET_GIDS);

if (list7 != null) {
    for (PackageInfo packageInfo : list7) {
          int[] gids = packageInfo.gids;
          if (gids != null && gids.length != 0) {
               for (int i = 0; i < gids.length; i++) {
                        Log.i("packageInfo-", "gids:" + i + "=" + gids[i]);
                }
          }
     }

}
2.4 PackageManager.GET_CONFIGURATIONS

    /**
     * {@link PackageInfo} flag: return information about
     * instrumentation in the package in
     * {@link PackageInfo#instrumentation}.
     */
    public static final int GET_INSTRUMENTATION         = 0x00000010;

也就是当在获取PackageInfo时,传入的过滤条件为 PackageManager.GET_INSTRUMENTATION,那么我们获取到应用程序所有的 清单中配制的所有的 Instrumentation 信息


List<PackageInfo> list8 = packageManager.getInstalledPackages(PackageManager.GET_INSTRUMENTATION);
if (list8 != null) {
    for (PackageInfo packageInfo : list8) {

        InstrumentationInfo[] instrumentation = packageInfo.instrumentation;

        if (instrumentation != null) {
            for (InstrumentationInfo instrumentationInfo : instrumentation) {
                Log.i("instrumentationInfo-", "instrumentationInfo " + instrumentationInfo);
            }
        }
    }
}
2.5 PackageManager.GET_INTENT_FILTERS

    /**
     * {@link PackageInfo} flag: return information about the
     * intent filters supported by the activity.
     */
    public static final int GET_INTENT_FILTERS          = 0x00000020;

也就是当在获取PackageInfo时,传入的过滤条件为 PackageManager.GET_INTENT_FILTERS,那么我们可以获取到清单文件下配制的activity 中的intent-filter 过滤条件信息



List<PackageInfo> list9 = packageManager.getInstalledPackages(PackageManager.GET_INTENT_FILTERS);
if (list9 != null) {
    for (PackageInfo packageInfo : list9) {
        FeatureInfo[] reqFeatures = packageInfo.reqFeatures;
        if (reqFeatures != null && reqFeatures.length != 0) {
            for (int i = 0; i < reqFeatures.length; i++) {
                Log.i("packageInfo-", "reqFeatures:" + i + "=" + reqFeatures[i]);
            }
        }
    }
}
2.6 PackageManager.GET_SIGNATURES

/**
 * {@link PackageInfo} flag: return information about the
 * signatures included in the package.
 */
public static final int GET_SIGNATURES          = 0x00000040;

也就是当在获取PackageInfo时,传入的过滤条件为 PackageManager.GET_SIGNATURES,那么我们可以获取到应用程序的签名信息

List<PackageInfo> list10 = packageManager.getInstalledPackages(PackageManager.GET_SIGNATURES);

if (list10 != null) {
    for (PackageInfo packageInfo : list10) {
        Signature[] signatures = packageInfo.signatures;
        if (signatures != null && signatures.length != 0) {
            for (int i = 0; i < signatures.length; i++) {
                Log.i("packageInfo-", "signatures:" + i + "=" + signatures[i]);
            }
        }
    }
}

2.7 PackageManager.GET_META_DATA

/**
 * {@link ComponentInfo} flag: return the {@link ComponentInfo#metaData}
 * data {@link android.os.Bundle}s that are associated with a component.
 * This applies for any API returning a ComponentInfo subclass.
 */
public static final int GET_META_DATA               = 0x00000080;

也就是当在获取PackageInfo时,传入的过滤条件为 PackageManager.GET_META_DATA ,那么我们可以获取到应用程序的清单文件中配制的所有的 meta-data 标签信息

List<PackageInfo> list10 = packageManager.getInstalledPackages(PackageManager.GET_META_DATA);


2.8 PackageManager.GET_PERMISSIONS

/**
 * {@link PackageInfo} flag: return information about
 * permissions in the package in
 * {@link PackageInfo#permissions}.
 */public static final int GET_PERMISSIONS               = 0x00001000;


也就是当在获取PackageInfo时,传入的过滤条件为 PackageManager.GET_PERMISSIONS ,那么我们可以获取到应用程序的清单文件中配制的所有的 权限信息


List<PackageInfo> list10 = packageManager.getInstalledPackages(PackageManager.GET_PERMISSIONS);
if (list10 != null) {
    for (PackageInfo packageInfo : list10) {

        String[] requestedPermissions = packageInfo.requestedPermissions;
        if (requestedPermissions != null && requestedPermissions.length != 0) {
            for (int i = 0; i < requestedPermissions.length; i++) {
                Log.i("packageInfo-", "requestedPermissions:" + i + "=" + requestedPermissions[i]);
            }
        }
    }
}

2.9 PackageManager.GET_PROVIDERS

/**
 * {@link PackageInfo} flag: return information about
 * content providers in the package in
 * {@link PackageInfo#providers}.
 */
public static final int GET_PROVIDERS               = 0x00000008;

也就是当在获取PackageInfo时,传入的过滤条件为 PackageManager.GET_PROVIDERS ,那么我们可以获取到应用程序的清单文件中配制的所有的 内容提供者

List<PackageInfo> list13 = packageManager.getInstalledPackages(PackageManager.GET_PROVIDERS);
if (list13 != null) {
    for (PackageInfo packageInfo : list13) {

        ProviderInfo[] providers = packageInfo.providers;
        if (providers != null) {
            for (ProviderInfo provider : providers) {

            }
        }
    }
}

2.10 PackageManager.GET_SERVICES

/**
 * {@link PackageInfo} flag: return information about
 * services in the package in {@link PackageInfo#services}.
 */
public static final int GET_SERVICES                = 0x00000004;

也就是当在获取PackageInfo时,传入的过滤条件为 PackageManager.GET_SERVICES ,那么我们可以获取到应用程序的清单文件中配制的所有的 服务

List<PackageInfo> installedPackageList2 = packageManager.getInstalledPackages(PackageManager.GET_SERVICES);
for (PackageInfo packageInfo : installedPackageList2) {
    // 获取 <application>标签下的所有services标签
    ServiceInfo[] services = packageInfo.services;
    if (services != null && services.length != 0) {
        for (int i = 0; i < services.length; i++) {
            Log.i("packageInfo-services", "services:" + i + "=" + services[i]);
        }
    }
}


2.11 PackageManager.GET_RECEIVERS

/**
 * {@link PackageInfo} flag: return information about
 * intent receivers in the package in
 * {@link PackageInfo#receivers}.
 */
public static final int GET_RECEIVERS               = 0x00000002;

也就是当在获取PackageInfo时,传入的过滤条件为 PackageManager.GET_RECEIVERS ,那么我们可以获取到应用程序的清单文件中配制的所有的 广播接收者


List<PackageInfo> installedPackageList3 = packageManager.getInstalledPackages(PackageManager.GET_RECEIVERS);
for (PackageInfo packageInfo : installedPackageList3) {
    // 获取 <application>标签下的所有receive标签
    ActivityInfo[] receivers = packageInfo.receivers;
    if (receivers != null && receivers.length != 0) {
        for (int i = 0; i < receivers.length; i++) {
            Log.i("packageInfo-", "receivers:" + i + "=" + receivers[i]);
        }
    }
}


2.12 PackageManager.GET_SHARED_LIBRARY_FILES

/**
 * {@link ApplicationInfo} flag: return the
 * {@link ApplicationInfo#sharedLibraryFiles paths to the shared libraries}
 * that are associated with an application.
 * This applies for any API returning an ApplicationInfo class, either
 * directly or nested inside of another.
 */
public static final int GET_SHARED_LIBRARY_FILES    = 0x00000400;

List<PackageInfo> list = packageManager.getInstalledPackages(PackageManager.GET_SHARED_LIBRARY_FILES);
2.13 PackageManager.GET_SHARED_LIBRARY_FILES

/**
 * {@link ApplicationInfo} flag: return the
 * {@link ApplicationInfo#sharedLibraryFiles paths to the shared libraries}
 * that are associated with an application.
 * This applies for any API returning an ApplicationInfo class, either
 * directly or nested inside of another.
 */
public static final int GET_SHARED_LIBRARY_FILES    = 0x00000400;

List<PackageInfo> list = packageManager.getInstalledPackages(PackageManager.GET_SHARED_LIBRARY_FILES);
早起的年轻人 CSDN认证博客专家 移动开发 项目管理 Java
只要用心去做,每一件事情还是有可能成功的,当然成功是没有界限的,只不过是达到自己心里的那个目标,公众号:我的大前端生涯,一个爱喝茶的程序员,通常会搞搞SpringBoot 、Herbinate、Mybatiys、Android、iOS、Flutter、Vue、小程序等.
©️2020 CSDN 皮肤主题: 代码科技 设计师:Amelia_0503 返回首页