public void parseApk(Context context, String apkFilePath){
PackageManager packageManager = context.getPackageManager();
PackageInfo packageInfo = packageManager.getPackageArchiveInfo(apkFilePath, 0);
if(packageInfo != null){
packageInfo.applicationInfo.sourceDir = apkFilePath;
packageInfo.applicationInfo.publicSourceDir = apkFilePath;
String name = packageInfo.applicationInfo.loadLabel(packageManager).toString();
String packageName = packageInfo.packageName;
String versionName = packageInfo.versionName;
int versionCode = packageInfo.versionCode;
Drawable iconDrawable = packageInfo.applicationInfo.loadIcon(packageManager);
}
}
需要注意的是在得到packageInfo后必须要设置applicationInfo的sourceDir和publicSourceDir,否则你在读取label的时候会输出如下错误信息并抛出异常
Failure retrieving text 0x7f050000 in package com.taskkiller.demo
你读icon的时候会输出如下错误信息并抛出异常
Failure retrieving icon 0x7f020005 in package com.sample.radio
这是因为label和icon都是在资源里面的,不设置sourceDir和publicSourceDir就不能读取资源里面的信息
原文地址:http://blog.csdn.net/u013308121/article/details/43673549