Android 获取未安装apk的name、icon等信息

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();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值