android studio实现高德地图api定位

简单的demo:https://my.oschina.net/zhangqie/blog/845488

SHA1值的获取:参考高德官网:http://lbs.amap.com/faq/top/hot-questions/253/

publicstatic String sHA1(Context context) {
    try {
        PackageInfo info = context.getPackageManager().getPackageInfo(
            context.getPackageName(), PackageManager.GET_SIGNATURES);
        byte[] cert = info.signatures[0].toByteArray();
        MessageDigest md = MessageDigest.getInstance("SHA1");
        byte[] publicKey = md.digest(cert);
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < publicKey.length; i++) {
            String appendString = Integer.toHexString(0xFF & publicKey[i])
                .toUpperCase(Locale.US);
            if (appendString.length() == 1)
                hexString.append("0");
                hexString.append(appendString);
                hexString.append(":");
        }
        String result = hexString.toString();
        return result.substring(0, result.length()-1);
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return null;
}

注意要去掉得到的result的最后的:

你可能感兴趣的:(java)