Android PackageManager的妙用

判断是否是系统应用

	private boolean isSystemApp(String pkgName) {
        if (null == pkgName || TextUtils.isEmpty(pkgName)) {
            return false;
        }

        try {
            PackageInfo info = getPackageManager().getPackageInfo(pkgName, PackageManager.GET_SIGNATURES);
            if ((info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
                return true;
            }

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        return false;
    }

判断是否是persist应用

	private boolean isPersistApp(String pkgName) {
        if (null == pkgName || TextUtils.isEmpty(pkgName)) {
            return false;
        }

        try {
            PackageInfo info = getPackageManager().getPackageInfo(pkgName, PackageManager.GET_SIGNATURES);

            if ((info.applicationInfo.flags &
                    (ApplicationInfo.FLAG_SYSTEM | ApplicationInfo.FLAG_PERSISTENT)) ==
                    (ApplicationInfo.FLAG_SYSTEM | ApplicationInfo.FLAG_PERSISTENT)) {
                return true;
            }

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        return false;
    }

获取签名信息

	try {
            PackageInfo info = getPackageManager().getPackageInfo("com.android.settings", PackageManager.GET_SIGNATURES);
            Signature[] signatures = info.signatures;
            String str = signatures[0].toString();
        } catch (Exception e){
            e.printStackTrace();
        }

str 输出为 :android.content.pm.Signature@b2d95fc0

获取 MD5、 SHA1、 SHA256

获取方法是一样的,只是初始化 MessageDigest 时传的值不同,

	/**
     * 获取 MD5 、SHA1 、SHA256
     * @param algorithm : 只能是 MD5 、SHA1 、SHA256
     * 返回值是这种形式:22:13:F1:91:ED:BC:92:E7:44:1B:34:9E:45:90:AB:76
     * */
    private String getAppSignatureAlgorithm(String pkgName, String algorithm){
        if (null == pkgName || TextUtils.isEmpty(pkgName)) {
            return null;
        }
        try {
            PackageInfo info = getPackageManager().getPackageInfo(pkgName, PackageManager.GET_SIGNATURES);

            Signature[] signatures = info.signatures;
            byte[] signBytes = signatures[0].toByteArray();

            MessageDigest mDigest = MessageDigest.getInstance(algorithm);
            byte[] bytesMd5 = mDigest.digest(signBytes);
            StringBuilder builder = new StringBuilder();
            for (byte b : bytesMd5) {
                builder.append(String.format("%02X", b)).append(":");// byte 转 16 进制
            }

            builder.replace(builder.length()-1, builder.length(),"");// 删除最后的 :
            return builder.toString();

        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }

使用示例,

String PKG_SETTINGS = "com.android.settings";
String sMd5 = getAppSignatureAlgorithm(PKG_SETTINGS, "MD5");
String sSha1 = getAppSignatureAlgorithm(PKG_SETTINGS, "SHA1");
String sSha256 = getAppSignatureAlgorithm(PKG_SETTINGS, "SHA256");

结果可以参考 AndroidStudio查看apk签名信息_android studio查看apk签名版本-CSDN博客 验证

你可能感兴趣的:(Android,PackageManager,android)