获取APK文件的签名信息,反射实现
1 private String showUninstallAPKSignatures(String apkPath) {
2 String PATH_PackageParser = "android.content.pm.PackageParser";
3 try {
4 // apk包的文件路径
5 // 这是一个Package 解释器, 是隐藏的
6 // 构造函数的参数只有一个, apk文件的路径
7 // PackageParser packageParser = new PackageParser(apkPath);
8 Class pkgParserCls = Class.forName(PATH_PackageParser);
9 Class[] typeArgs = new Class[1];
10 typeArgs[0] = String.class;
11 Constructor pkgParserCt = pkgParserCls.getConstructor(typeArgs);
12 Object[] valueArgs = new Object[1];
13 valueArgs[0] = apkPath;
14 Object pkgParser = pkgParserCt.newInstance(valueArgs);
15 MediaApplication.logD(DownloadApk.class, "pkgParser:" + pkgParser.toString());
16 // 这个是与显示有关的, 里面涉及到一些像素显示等等, 我们使用默认的情况
17 DisplayMetrics metrics = new DisplayMetrics();
18 metrics.setToDefaults();
19 // PackageParser.Package mPkgInfo = packageParser.parsePackage(new
20 // File(apkPath), apkPath,
21 // metrics, 0);
22 typeArgs = new Class[4];
23 typeArgs[0] = File.class;
24 typeArgs[1] = String.class;
25 typeArgs[2] = DisplayMetrics.class;
26 typeArgs[3] = Integer.TYPE;
27 Method pkgParser_parsePackageMtd = pkgParserCls.getDeclaredMethod("parsePackage",
28 typeArgs);
29 valueArgs = new Object[4];
30 valueArgs[0] = new File(apkPath);
31 valueArgs[1] = apkPath;
32 valueArgs[2] = metrics;
33 valueArgs[3] = PackageManager.GET_SIGNATURES;
34 Object pkgParserPkg = pkgParser_parsePackageMtd.invoke(pkgParser, valueArgs);
35
36 typeArgs = new Class[2];
37 typeArgs[0] = pkgParserPkg.getClass();
38 typeArgs[1] = Integer.TYPE;
39 Method pkgParser_collectCertificatesMtd = pkgParserCls.getDeclaredMethod("collectCertificates",
40 typeArgs);
41 valueArgs = new Object[2];
42 valueArgs[0] = pkgParserPkg;
43 valueArgs[1] = PackageManager.GET_SIGNATURES;
44 pkgParser_collectCertificatesMtd.invoke(pkgParser, valueArgs);
45 // 应用程序信息包, 这个公开的, 不过有些函数, 变量没公开
46 Field packageInfoFld = pkgParserPkg.getClass().getDeclaredField("mSignatures");
47 Signature[] info = (Signature[]) packageInfoFld.get(pkgParserPkg);
48 MediaApplication.logD(DownloadApk.class, "size:"+info.length);
49 MediaApplication.logD(DownloadApk.class, info[0].toCharsString());
50 return info[0].toCharsString();
51 } catch (Exception e) {
52 e.printStackTrace();
53 }
54 return null;
55 }
2 String PATH_PackageParser = "android.content.pm.PackageParser";
3 try {
4 // apk包的文件路径
5 // 这是一个Package 解释器, 是隐藏的
6 // 构造函数的参数只有一个, apk文件的路径
7 // PackageParser packageParser = new PackageParser(apkPath);
8 Class pkgParserCls = Class.forName(PATH_PackageParser);
9 Class[] typeArgs = new Class[1];
10 typeArgs[0] = String.class;
11 Constructor pkgParserCt = pkgParserCls.getConstructor(typeArgs);
12 Object[] valueArgs = new Object[1];
13 valueArgs[0] = apkPath;
14 Object pkgParser = pkgParserCt.newInstance(valueArgs);
15 MediaApplication.logD(DownloadApk.class, "pkgParser:" + pkgParser.toString());
16 // 这个是与显示有关的, 里面涉及到一些像素显示等等, 我们使用默认的情况
17 DisplayMetrics metrics = new DisplayMetrics();
18 metrics.setToDefaults();
19 // PackageParser.Package mPkgInfo = packageParser.parsePackage(new
20 // File(apkPath), apkPath,
21 // metrics, 0);
22 typeArgs = new Class[4];
23 typeArgs[0] = File.class;
24 typeArgs[1] = String.class;
25 typeArgs[2] = DisplayMetrics.class;
26 typeArgs[3] = Integer.TYPE;
27 Method pkgParser_parsePackageMtd = pkgParserCls.getDeclaredMethod("parsePackage",
28 typeArgs);
29 valueArgs = new Object[4];
30 valueArgs[0] = new File(apkPath);
31 valueArgs[1] = apkPath;
32 valueArgs[2] = metrics;
33 valueArgs[3] = PackageManager.GET_SIGNATURES;
34 Object pkgParserPkg = pkgParser_parsePackageMtd.invoke(pkgParser, valueArgs);
35
36 typeArgs = new Class[2];
37 typeArgs[0] = pkgParserPkg.getClass();
38 typeArgs[1] = Integer.TYPE;
39 Method pkgParser_collectCertificatesMtd = pkgParserCls.getDeclaredMethod("collectCertificates",
40 typeArgs);
41 valueArgs = new Object[2];
42 valueArgs[0] = pkgParserPkg;
43 valueArgs[1] = PackageManager.GET_SIGNATURES;
44 pkgParser_collectCertificatesMtd.invoke(pkgParser, valueArgs);
45 // 应用程序信息包, 这个公开的, 不过有些函数, 变量没公开
46 Field packageInfoFld = pkgParserPkg.getClass().getDeclaredField("mSignatures");
47 Signature[] info = (Signature[]) packageInfoFld.get(pkgParserPkg);
48 MediaApplication.logD(DownloadApk.class, "size:"+info.length);
49 MediaApplication.logD(DownloadApk.class, info[0].toCharsString());
50 return info[0].toCharsString();
51 } catch (Exception e) {
52 e.printStackTrace();
53 }
54 return null;
55 }
获取程序自身的签名:
private String getSign(Context context) {
PackageManager pm = context.getPackageManager();
List<PackageInfo> apps = pm.getInstalledPackages(PackageManager.GET_SIGNATURES);
Iterator<PackageInfo> iter = apps.iterator();
while(iter.hasNext()) {
PackageInfo packageinfo = iter.next();
String packageName = packageinfo.packageName;
if (packageName.equals(instance.getPackageName())) {
MediaApplication.logD(DownloadApk.class, packageinfo.signatures[0].toCharsString());
return packageinfo.signatures[0].toCharsString();
}
}
return null;
}
PackageManager pm = context.getPackageManager();
List<PackageInfo> apps = pm.getInstalledPackages(PackageManager.GET_SIGNATURES);
Iterator<PackageInfo> iter = apps.iterator();
while(iter.hasNext()) {
PackageInfo packageinfo = iter.next();
String packageName = packageinfo.packageName;
if (packageName.equals(instance.getPackageName())) {
MediaApplication.logD(DownloadApk.class, packageinfo.signatures[0].toCharsString());
return packageinfo.signatures[0].toCharsString();
}
}
return null;
}
对比2个方法的返回值来判断APK升级包的签名是否一致,一致就提示可以安装。