|
9 | 9 | import android.content.pm.Signature; |
10 | 10 | import android.graphics.drawable.Drawable; |
11 | 11 |
|
| 12 | +import java.security.MessageDigest; |
| 13 | +import java.security.NoSuchAlgorithmException; |
12 | 14 | import java.io.BufferedReader; |
13 | 15 | import java.io.File; |
14 | 16 | import java.io.IOException; |
@@ -30,6 +32,36 @@ public class AppUtils { |
30 | 32 | private AppUtils() { |
31 | 33 | throw new UnsupportedOperationException("u can't instantiate me..."); |
32 | 34 | } |
| 35 | + |
| 36 | + |
| 37 | + /** |
| 38 | + * 获取应用的 SHA1 值, 可据此判断高德,百度地图 key 是否正确 |
| 39 | + * @param context 上下文 |
| 40 | + * @return 应用的 SHA1 字符串, 比如: 53:FD:54:DC:19:0F:11:AC:B5:22:9E:F1:1A:68:88:1B:8B:E8:54:42 |
| 41 | + */ |
| 42 | + public static String getSHA1(Context context) { |
| 43 | + try { |
| 44 | + PackageInfo info = context.getPackageManager().getPackageInfo( |
| 45 | + context.getPackageName(), PackageManager.GET_SIGNATURES); |
| 46 | + byte[] cert = info.signatures[0].toByteArray(); |
| 47 | + MessageDigest md = MessageDigest.getInstance("SHA1"); |
| 48 | + byte[] publicKey = md.digest(cert); |
| 49 | + StringBuilder hexString = new StringBuilder(); |
| 50 | + for (byte aPublicKey : publicKey) { |
| 51 | + String appendString = Integer.toHexString(0xFF & aPublicKey).toUpperCase(); |
| 52 | + if (appendString.length() == 1) |
| 53 | + hexString.append("0"); |
| 54 | + hexString.append(appendString); |
| 55 | + hexString.append(":"); |
| 56 | + } |
| 57 | + String result = hexString.toString(); |
| 58 | + return result.substring(0, result.length() - 1); |
| 59 | + } catch (PackageManager.NameNotFoundException | NoSuchAlgorithmException e) { |
| 60 | + e.printStackTrace(); |
| 61 | + } |
| 62 | + return ""; |
| 63 | + } |
| 64 | + |
33 | 65 |
|
34 | 66 | /** |
35 | 67 | * 判断App是否安装 |
@@ -628,4 +660,4 @@ public static boolean cleanAppData(Context context, File... dirs) { |
628 | 660 | } |
629 | 661 | return isSuccess; |
630 | 662 | } |
631 | | -} |
| 663 | +} |