PackageManager从字面意义上看也很容易理解它的作用,他肯定是跟包管理有关系,何为包管理?官方文档给出的解释是:你可以检索各种和应用的包有关系的信息。那么下面我们就来看看它是怎么提供获取信息的方式的,以及这些信息是什么?
首先跟ActivityManager一样,先创建一个项目,然后在activity中onCreate()方法中创建PackageManager对象:
PackageManager packageManager = getPackageManager();
ApplicationInfo为AndroidManifest.xml里的<application>标签里的各个属性值。
// GET_META_DATA=128,GET_SHARED_LIBRARY_FILES = 1024, // GET_UNINSTALLED_PACKAGES =8196 List<ApplicationInfo> list1 = packageManager.getInstalledApplications(128); if (list1.size() != 0) { for (ApplicationInfo info : list1) { // 配置文件中的android:backupAgent属性值,用于备份 String backupAgentName = info.backupAgentName; Log.i(TAG, "backupAgentName: " + backupAgentName); // 获取继承Application类的对象,维护全局的Application状态 //但一般都不用继承的方式,可以通过Context.getApplicationContext()方法得到 String className = info.className; Log.i(TAG, "className: " + className); // 包名 String packageName = info.packageName; Log.i(TAG, "packageName: " + packageName); // <android:manageSpaceActivity>标签的值 //该标签代表了指定一个activity来处理用户的空间数据。而不是通过设置里的一个按钮来操作 String activityName = info.manageSpaceActivityName; Log.i(TAG, "activityName: " + activityName); // 应用所在的进程 String processName = info.processName; Log.i(TAG, "processName: " + processName); // 权限 String permisson = info.permission; Log.i(TAG, "permisson: " + permisson); // 创建对象时,传入的是GET_SHARED_LIBRARY_FILES该属性才有值 String[] files = info.sharedLibraryFiles; Log.i(TAG, "files: " + files); // 存放数据的路径 String dataPath = info.dataDir; Log.i(TAG, "dataPath: " + dataPath); // 本地路径 String nativePath = info.nativeLibraryDir; Log.i(TAG, "nativePath:" + nativePath); // 公共资源路径 String punlicSourcePath = info.publicSourceDir; Log.i(TAG, "punlicSourcePath: " + punlicSourcePath); // 资源路径 String sourcePath = info.sourceDir; Log.i(TAG, "sourcePath: " + sourcePath); // 内同的activity的task名称 String taskAffinity = info.taskAffinity; Log.i(TAG, "taskAffinity: " + taskAffinity); // 如果是false,代表application里的所有组件都禁用 boolean enable = info.enabled; Log.i(TAG, "enable: " + enable); // 表述资源文件的标识 int descriRes = info.descriptionRes; Log.i(TAG, "descriRes: " + descriRes); int flag = info.flags; Log.i(TAG, "flag: " + flag); // 指定smallest screen width的值,超过这个值,就要开启屏幕兼容 int compatibleWidth = info.compatibleWidthLimitDp;//android:compatibleWidthLimitDp属性 Log.i(TAG, "compatibleWidth: " + compatibleWidth); // 同上,只是这时候用户无法禁止屏幕兼容模式,说明是强制启动屏幕兼容 int largestWidth = info.largestWidthLimitDp;//android:largestWidthLimitDp属性 Log.i(TAG, "largestWidth: " + largestWidth); // 所需屏幕空间的最短尺寸, int samllestWidth = info.requiresSmallestWidthDp;//android:requiresSmallestWidthDp属性 Log.i(TAG, "samllestWidth: " + samllestWidth); // 应用所需的最小sdk版本 int sdkVersion = info.targetSdkVersion; Log.i(TAG, "sdkVersion: " + sdkVersion); int theme = info.theme; Log.i(TAG, "theme: " + theme);//android:theme= int uid = info.uid; Log.i(TAG, "uid: " + uid); // 配置文件中的uiOptions属性的值 int uiOptions = info.uiOptions; Log.i(TAG, "uiOptions: " + uiOptions); Log.i(TAG, "======================================="); } }
获得ApplicationInfo信息的方法,需要传入int类型的参数,有3中组合:GET_META_DATA 、GET_SHARE_LIBRARY_FILES|、GET_UNINSTALLED_PACKAGES.传入的参数不同,得到的信息也就不同。首先我们传入128,也就是GET_META_DATA.运行程序得到log输出信息如下:
03-28 15:20:03.763: I/MainActivity(7785): backupAgentName: null 03-28 15:20:03.763: I/MainActivity(7785): className: null 03-28 15:20:03.763: I/MainActivity(7785): packageName: com.android.defcontainer 03-28 15:20:03.763: I/MainActivity(7785): activityName: null 03-28 15:20:03.763: I/MainActivity(7785): processName: com.android.defcontainer 03-28 15:20:03.763: I/MainActivity(7785): permisson: null 03-28 15:20:03.763: I/MainActivity(7785): files: null 03-28 15:20:03.763: I/MainActivity(7785): dataPath: /data/data/com.android.defcontainer 03-28 15:20:03.763: I/MainActivity(7785): nativePath:/data/app-lib/DefaultContainerService 03-28 15:20:03.763: I/MainActivity(7785): punlicSourcePath: /system/priv-app/DefaultContainerService.apk 03-28 15:20:03.773: I/MainActivity(7785): sourcePath: /system/priv-app/DefaultContainerService.apk 03-28 15:20:03.773: I/MainActivity(7785): taskAffinity: com.android.defcontainer 03-28 15:20:03.773: I/MainActivity(7785): enable: true 03-28 15:20:03.773: I/MainActivity(7785): descriRes: 0 03-28 15:20:03.773: I/MainActivity(7785): flag: 1082670661 03-28 15:20:03.773: I/MainActivity(7785): compatibleWidth: 0 03-28 15:20:03.773: I/MainActivity(7785): largestWidth: 0 03-28 15:20:03.773: I/MainActivity(7785): samllestWidth: 0 03-28 15:20:03.773: I/MainActivity(7785): sdkVersion: 19 03-28 15:20:03.773: I/MainActivity(7785): theme: 0 03-28 15:20:03.773: I/MainActivity(7785): uid: 10004 03-28 15:20:03.773: I/MainActivity(7785): uiOptions: 0 03-28 15:20:03.783: I/MainActivity(7785): ======================================= 03-28 15:20:03.783: I/MainActivity(7785): backupAgentName: null 03-28 15:20:03.783: I/MainActivity(7785): className: null 03-28 15:20:03.783: I/MainActivity(7785): packageName: com.sprd.carddav_sync 03-28 15:20:03.783: I/MainActivity(7785): activityName: null 03-28 15:20:03.783: I/MainActivity(7785): processName: com.sprd.carddav_sync 03-28 15:20:03.783: I/MainActivity(7785): permisson: null 03-28 15:20:03.783: I/MainActivity(7785): files: null 03-28 15:20:03.783: I/MainActivity(7785): dataPath: /data/data/com.sprd.carddav_sync 03-28 15:20:03.783: I/MainActivity(7785): nativePath:/data/app-lib/Carddav-Sync 03-28 15:20:03.783: I/MainActivity(7785): punlicSourcePath: /system/app/Carddav-Sync.apk 03-28 15:20:03.783: I/MainActivity(7785): sourcePath: /system/app/Carddav-Sync.apk 03-28 15:20:03.783: I/MainActivity(7785): taskAffinity: com.sprd.carddav_sync 03-28 15:20:03.783: I/MainActivity(7785): enable: true 03-28 15:20:03.783: I/MainActivity(7785): descriRes: 0 03-28 15:20:03.783: I/MainActivity(7785): flag: 8961605 03-28 15:20:03.783: I/MainActivity(7785): compatibleWidth: 0 03-28 15:20:03.783: I/MainActivity(7785): largestWidth: 0 03-28 15:20:03.783: I/MainActivity(7785): samllestWidth: 0 03-28 15:20:03.783: I/MainActivity(7785): sdkVersion: 16 03-28 15:20:03.783: I/MainActivity(7785): theme: 2131230721 03-28 15:20:03.793: I/MainActivity(7785): uid: 10022 03-28 15:20:03.793: I/MainActivity(7785): uiOptions: 0 03-28 15:20:03.793: I/MainActivity(7785): ======================================= 03-28 15:20:03.793: I/MainActivity(7785): backupAgentName: null 03-28 15:20:03.793: I/MainActivity(7785): className: com.sohu.newsclient.NewsApplication 03-28 15:20:03.793: I/MainActivity(7785): packageName: com.sohu.newsclient 03-28 15:20:03.793: I/MainActivity(7785): activityName: null 03-28 15:20:03.793: I/MainActivity(7785): processName: com.sohu.newsclient 03-28 15:20:03.793: I/MainActivity(7785): permisson: null 03-28 15:20:03.793: I/MainActivity(7785): files: null 03-28 15:20:03.793: I/MainActivity(7785): dataPath: /data/data/com.sohu.newsclient 03-28 15:20:03.793: I/MainActivity(7785): nativePath:/data/app-lib/SohuNewsClient 03-28 15:20:03.793: I/MainActivity(7785): punlicSourcePath: /system/preloadapp/SohuNewsClient.apk 03-28 15:20:03.793: I/MainActivity(7785): sourcePath: /system/preloadapp/SohuNewsClient.apk 03-28 15:20:03.793: I/MainActivity(7785): taskAffinity: com.sohu.newsclient 03-28 15:20:03.793: I/MainActivity(7785): enable: true 03-28 15:20:03.793: I/MainActivity(7785): descriRes: 0 03-28 15:20:03.793: I/MainActivity(7785): flag: 8437316 03-28 15:20:03.793: I/MainActivity(7785): compatibleWidth: 0 03-28 15:20:03.793: I/MainActivity(7785): largestWidth: 0 03-28 15:20:03.793: I/MainActivity(7785): samllestWidth: 0 03-28 15:20:03.793: I/MainActivity(7785): sdkVersion: 5 03-28 15:20:03.793: I/MainActivity(7785): theme: 0 03-28 15:20:03.793: I/MainActivity(7785): uid: 10063 03-28 15:20:03.793: I/MainActivity(7785): uiOptions: 0 03-28 15:20:03.793: I/MainActivity(7785): ======================================= 03-28 15:20:03.793: I/MainActivity(7785): backupAgentName: null 03-28 15:20:03.793: I/MainActivity(7785): className: com.android.quicksearchbox.QsbApplicationWrapper 03-28 15:20:03.803: I/MainActivity(7785): packageName: com.android.quicksearchbox 03-28 15:20:03.803: I/MainActivity(7785): activityName: null 03-28 15:20:03.803: I/MainActivity(7785): processName: com.android.quicksearchbox 03-28 15:20:03.803: I/MainActivity(7785): permisson: null 03-28 15:20:03.803: I/MainActivity(7785): files: null 03-28 15:20:03.803: I/MainActivity(7785): dataPath: /data/data/com.android.quicksearchbox 03-28 15:20:03.803: I/MainActivity(7785): nativePath:/data/app-lib/SprdQuickSearchBox 03-28 15:20:03.803: I/MainActivity(7785): punlicSourcePath: /system/app/SprdQuickSearchBox.apk 03-28 15:20:03.803: I/MainActivity(7785): sourcePath: /system/app/SprdQuickSearchBox.apk 03-28 15:20:03.803: I/MainActivity(7785): taskAffinity: com.android.quicksearchbox 03-28 15:20:03.803: I/MainActivity(7785): enable: true 03-28 15:20:03.803: I/MainActivity(7785): descriRes: 0 03-28 15:20:03.803: I/MainActivity(7785): flag: 8961605 03-28 15:20:03.803: I/MainActivity(7785): compatibleWidth: 0 03-28 15:20:03.803: I/MainActivity(7785): largestWidth: 0 03-28 15:20:03.803: I/MainActivity(7785): samllestWidth: 0 03-28 15:20:03.803: I/MainActivity(7785): sdkVersion: 19 03-28 15:20:03.803: I/MainActivity(7785): theme: 2131558413 03-28 15:20:03.803: I/MainActivity(7785): uid: 10054 03-28 15:20:03.803: I/MainActivity(7785): uiOptions: 0 03-28 15:20:03.803: I/MainActivity(7785): ======================================= 03-28 15:20:03.803: I/MainActivity(7785): backupAgentName: null 03-28 15:20:03.803: I/MainActivity(7785): className: null 03-28 15:20:03.803: I/MainActivity(7785): packageName: com.spreadst.drag 03-28 15:20:03.803: I/MainActivity(7785): activityName: null 03-28 15:20:03.803: I/MainActivity(7785): processName: com.spreadst.drag 03-28 15:20:03.803: I/MainActivity(7785): permisson: null 03-28 15:20:03.803: I/MainActivity(7785): files: null 03-28 15:20:03.803: I/MainActivity(7785): dataPath: /data/data/com.spreadst.drag 03-28 15:20:03.803: I/MainActivity(7785): nativePath:/data/app-lib/Draglockscreen 03-28 15:20:03.803: I/MainActivity(7785): punlicSourcePath: /system/app/Draglockscreen.apk 03-28 15:20:03.803: I/MainActivity(7785): sourcePath: /system/app/Draglockscreen.apk 03-28 15:20:03.803: I/MainActivity(7785): taskAffinity: com.spreadst.drag 03-28 15:20:03.803: I/MainActivity(7785): enable: true 03-28 15:20:03.803: I/MainActivity(7785): descriRes: 0 03-28 15:20:03.803: I/MainActivity(7785): flag: 8961605 03-28 15:20:03.803: I/MainActivity(7785): compatibleWidth: 0 03-28 15:20:03.803: I/MainActivity(7785): largestWidth: 0 03-28 15:20:03.803: I/MainActivity(7785): samllestWidth: 0 03-28 15:20:03.803: I/MainActivity(7785): sdkVersion: 19 03-28 15:20:03.803: I/MainActivity(7785): theme: 0 03-28 15:20:03.803: I/MainActivity(7785): uid: 10026 03-28 15:20:03.803: I/MainActivity(7785): uiOptions: 0 03-28 15:20:03.803: I/MainActivity(7785): ======================================= 03-28 15:20:03.803: I/MainActivity(7785): backupAgentName: null 03-28 15:20:03.803: I/MainActivity(7785): className: com.android.contacts.ContactsApplication 03-28 15:20:03.803: I/MainActivity(7785): packageName: com.android.contacts 03-28 15:20:03.803: I/MainActivity(7785): activityName: null 03-28 15:20:03.803: I/MainActivity(7785): processName: com.android.contacts 03-28 15:20:03.803: I/MainActivity(7785): permisson: null 03-28 15:20:03.803: I/MainActivity(7785): files: null 03-28 15:20:03.803: I/MainActivity(7785): dataPath: /data/data/com.android.contacts 03-28 15:20:03.803: I/MainActivity(7785): nativePath:/data/app-lib/Contacts 03-28 15:20:03.803: I/MainActivity(7785): punlicSourcePath: /system/priv-app/Contacts.apk 03-28 15:20:03.803: I/MainActivity(7785): sourcePath: /system/priv-app/Contacts.apk 03-28 15:20:03.803: I/MainActivity(7785): taskAffinity: android.task.contacts 03-28 15:20:03.803: I/MainActivity(7785): enable: true 03-28 15:20:03.803: I/MainActivity(7785): descriRes: 0 03-28 15:20:03.803: I/MainActivity(7785): flag: 1086897733 03-28 15:20:03.803: I/MainActivity(7785): compatibleWidth: 0 03-28 15:20:03.803: I/MainActivity(7785): largestWidth: 0 03-28 15:20:03.803: I/MainActivity(7785): samllestWidth: 0 03-28 15:20:03.803: I/MainActivity(7785): sdkVersion: 19 03-28 15:20:03.803: I/MainActivity(7785): theme: 0 03-28 15:20:03.803: I/MainActivity(7785): uid: 10003 03-28 15:20:03.803: I/MainActivity(7785): uiOptions: 0 03-28 15:20:03.803: I/MainActivity(7785): ======================================= 03-28 15:20:03.803: I/MainActivity(7785): backupAgentName: null 03-28 15:20:03.803: I/MainActivity(7785): className: com.android.phone.PhoneApp 03-28 15:20:03.803: I/MainActivity(7785): packageName: com.android.phone 03-28 15:20:03.803: I/MainActivity(7785): activityName: null 03-28 15:20:03.803: I/MainActivity(7785): processName: com.android.phone 03-28 15:20:03.803: I/MainActivity(7785): permisson: null 03-28 15:20:03.803: I/MainActivity(7785): files: null 03-28 15:20:03.803: I/MainActivity(7785): dataPath: /data/data/com.android.phone 03-28 15:20:03.803: I/MainActivity(7785): nativePath:/data/app-lib/TeleService 03-28 15:20:03.803: I/MainActivity(7785): punlicSourcePath: /system/priv-app/TeleService.apk 03-28 15:20:03.803: I/MainActivity(7785): sourcePath: /system/priv-app/TeleService.apk 03-28 15:20:03.803: I/MainActivity(7785): taskAffinity: com.android.phone 03-28 15:20:03.803: I/MainActivity(7785): enable: true 03-28 15:20:03.803: I/MainActivity(7785): descriRes: 0 03-28 15:20:03.803: I/MainActivity(7785): flag: 1086897741 03-28 15:20:03.803: I/MainActivity(7785): compatibleWidth: 0 03-28 15:20:03.803: I/MainActivity(7785): largestWidth: 0 03-28 15:20:03.803: I/MainActivity(7785): samllestWidth: 0 03-28 15:20:03.803: I/MainActivity(7785): sdkVersion: 19 03-28 15:20:03.803: I/MainActivity(7785): theme: 2131689509 03-28 15:20:03.803: I/MainActivity(7785): uid: 1001 03-28 15:20:03.803: I/MainActivity(7785): uiOptions: 0 03-28 15:20:03.803: I/MainActivity(7785): ======================================= 03-28 15:20:03.803: I/MainActivity(7785): backupAgentName: null 03-28 15:20:03.803: I/MainActivity(7785): className: null 03-28 15:20:03.803: I/MainActivity(7785): packageName: com.sprd.monkeytest 03-28 15:20:03.803: I/MainActivity(7785): activityName: null 03-28 15:20:03.803: I/MainActivity(7785): processName: com.sprd.monkeytest 03-28 15:20:03.803: I/MainActivity(7785): permisson: null 03-28 15:20:03.803: I/MainActivity(7785): files: null 03-28 15:20:03.803: I/MainActivity(7785): dataPath: /data/data/com.sprd.monkeytest 03-28 15:20:03.803: I/MainActivity(7785): nativePath:/data/app-lib/MonkeyTest 03-28 15:20:03.803: I/MainActivity(7785): punlicSourcePath: /system/app/MonkeyTest.apk 03-28 15:20:03.803: I/MainActivity(7785): sourcePath: /system/app/MonkeyTest.apk 03-28 15:20:03.803: I/MainActivity(7785): taskAffinity: com.sprd.monkeytest 03-28 15:20:03.803: I/MainActivity(7785): enable: true 03-28 15:20:03.803: I/MainActivity(7785): descriRes: 0 03-28 15:20:03.803: I/MainActivity(7785): flag: 8961605 03-28 15:20:03.803: I/MainActivity(7785): compatibleWidth: 0 03-28 15:20:03.803: I/MainActivity(7785): largestWidth: 0 03-28 15:20:03.803: I/MainActivity(7785): samllestWidth: 0 03-28 15:20:03.803: I/MainActivity(7785): sdkVersion: 19 03-28 15:20:03.803: I/MainActivity(7785): theme: 2131165185 03-28 15:20:03.803: I/MainActivity(7785): uid: 1000 03-28 15:20:03.803: I/MainActivity(7785): uiOptions: 0 03-28 15:20:03.803: I/MainActivity(7785): =======================================
我们传入GET_SHARE_LIBRARY_FILES看看得到什么结果:
List<ApplicationInfo> list1 = packageManager.getInstalledApplications(1024);
看看sharedLibraryFiles的值:
03-28 15:55:46.433: I/MainActivity(8616): files: [Ljava.lang.String;@4258a100
确实获得到了值,该字符串数组里存放的是共享库的路径。
从上面的方法所获得的值来说,ApplicationInfo对象里保存的信息都是<application>标签里的属性值。
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > </application>
就像上面的配置文件,android:theme、android:label这种标签里的值。