安卓(Android) 怎么获取卸载apk签名(Signature)

2019年10月17日21:51:30安卓(Android) 怎么获取卸载apk签名(Signature)已关闭评论 396 1756字阅读5分51秒

1.简介

安卓(Android) 怎么获取卸载apk签名(Signature)?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */



import android.content.pm.PackageManager;

import android.content.pm.Signature;
import android.util.DisplayMetrics;

import java.io.File;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Main {
    private static Signature[] getUninstallAPKSignatures(String apkPath) {
        String PATH_PackageParser = "android.content.pm.PackageParser";
        try {
            
            
            
            
            Class pkgParserCls = Class.forName(PATH_PackageParser);
            Class[] typeArgs = new Class[1];
            typeArgs[0] = String.class;
            Constructor pkgParserCt = pkgParserCls.getConstructor(typeArgs);
            Object[] valueArgs = new Object[1];
            valueArgs[0] = apkPath;
            Object pkgParser = pkgParserCt.newInstance(valueArgs);
            
            DisplayMetrics metrics = new DisplayMetrics();
            metrics.setToDefaults();
            
            
            
            typeArgs = new Class[4];
            typeArgs[0] = File.class;
            typeArgs[1] = String.class;
            typeArgs[2] = DisplayMetrics.class;
            typeArgs[3] = Integer.TYPE;
            Method pkgParser_parsePackageMtd = pkgParserCls
                    .getDeclaredMethod("parsePackage", typeArgs);
            valueArgs = new Object[4];
            valueArgs[0] = new File(apkPath);
            valueArgs[1] = apkPath;
            valueArgs[2] = metrics;
            valueArgs[3] = PackageManager.GET_SIGNATURES;
            Object pkgParserPkg = pkgParser_parsePackageMtd.invoke(
                    pkgParser, valueArgs);

            typeArgs = new Class[2];
            typeArgs[0] = pkgParserPkg.getClass();
            typeArgs[1] = Integer.TYPE;
            Method pkgParser_collectCertificatesMtd = pkgParserCls
                    .getDeclaredMethod("collectCertificates", typeArgs);
            valueArgs = new Object[2];
            valueArgs[0] = pkgParserPkg;
            valueArgs[1] = PackageManager.GET_SIGNATURES;
            pkgParser_collectCertificatesMtd.invoke(pkgParser, valueArgs);
            
            Field packageInfoFld = pkgParserPkg.getClass()
                    .getDeclaredField("mSignatures");
            Signature[] info = (Signature[]) packageInfoFld
                    .get(pkgParserPkg);
            return info;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

 
继续阅读
编程十万个怎么办