public void getUninstallApkInfo() {
String PATH_PackageParser = "android.content.pm.PackageParser";
String PATH_AssetManager = "android.content.res.AssetManager";
String apkPath = "sdcard/download/tieba.apk";
try {
Class pkgParserCls = Class.forName(PATH_PackageParser);
//Constructor[] constructors = pkgParserCls.getConstructors();
Constructor pkgParserCt = pkgParserCls.getConstructor((Class[]) null);
Object[] valueArgs = new Object[1];
valueArgs[0] = apkPath;
Object pkgParser = pkgParserCt.newInstance();
Log.d("GetUninstallApkInfo", "pkgParser:" + pkgParser.toString());
DisplayMetrics metrics = new DisplayMetrics();
metrics.setToDefaults();
Class[] typeArgs = new Class[2];
typeArgs[0] = File.class;
typeArgs[1] = Integer.TYPE;
//Method[] methods = pkgParserCls.getMethods();
Method pkgParser_parsePackageMtd = pkgParserCls.getDeclaredMethod("parsePackage", typeArgs);
valueArgs = new Object[2];
valueArgs[0] = new File(apkPath);
valueArgs[1] = 0;
Object pkgParserPkg = pkgParser_parsePackageMtd.invoke(pkgParser, valueArgs);
// 应用程序信息包, 这个公开的, 不过有些函数, 变量没公开
// ApplicationInfo info = mPkgInfo.applicationInfo;
Field appInfoFld = pkgParserPkg.getClass().getDeclaredField("applicationInfo");
ApplicationInfo info = (ApplicationInfo) appInfoFld.get(pkgParserPkg);
// uid 输出为"-1",原因是未安装,系统未分配其Uid。
Log.d("GetUninstallApkInfo", "pkg:" + info.packageName + " uid=" + info.uid);
Class assetMagCls = Class.forName(PATH_AssetManager);
Constructor assetMagCt = assetMagCls.getConstructor((Class[]) null);
Object assetMag = assetMagCt.newInstance((Object[]) null);
typeArgs = new Class[1];
typeArgs[0] = String.class;
Method assetMag_addAssetPathMtd = assetMagCls.getDeclaredMethod("addAssetPath",
typeArgs);
valueArgs = new Object[1];
valueArgs[0] = apkPath;
assetMag_addAssetPathMtd.invoke(assetMag, valueArgs);
Resources res = getResources();
typeArgs = new Class[3];
typeArgs[0] = assetMag.getClass();
typeArgs[1] = res.getDisplayMetrics().getClass();
typeArgs[2] = res.getConfiguration().getClass();
Constructor resCt = Resources.class.getConstructor(typeArgs);
valueArgs = new Object[3];
valueArgs[0] = assetMag;
valueArgs[1] = res.getDisplayMetrics();
valueArgs[2] = res.getConfiguration();
res = (Resources) resCt.newInstance(valueArgs);
CharSequence label = null;
if (info.labelRes != 0) {
label = res.getText(info.labelRes);
}
Log.d("GetUninstallApkInfo", "label=" + label);
// 这里就是读取一个apk程序的图标
if (info.icon != 0) {
Drawable icon = res.getDrawable(info.icon);
}
} catch (Exception e) {
e.printStackTrace();
}
}
Android 获取未安装apk的name、icon等信息
最新推荐文章于 2021-05-28 23:51:33 发布