Android 获取唯一标识替代方法

private static String getTheOnlyID() {
String onlyOne;
//获取IMEI
TelephonyManager TelephonyMgr = (TelephonyManager) Utils.getContext().getSystemService(TELEPHONY_SERVICE);
@SuppressLint("HardwareIds") String szImei = TelephonyMgr.getDeviceId();
//读取手机设备信息,拼接成一个15位数据
String m_szDevIDShort = "35" + //we make this look like a valid IMEI
Build.BOARD.length() % 10 +
Build.BRAND.length() % 10 +
Build.CPU_ABI.length() % 10 +
Build.DEVICE.length() % 10 +
Build.DISPLAY.length() % 10 +
Build.HOST.length() % 10 +
Build.ID.length() % 10 +
Build.MANUFACTURER.length() % 10 +
Build.MODEL.length() % 10 +
Build.PRODUCT.length() % 10 +
Build.TAGS.length() % 10 +
Build.TYPE.length() % 10 +
Build.USER.length() % 10;
//进行md5加密,生成16位字符
String md5Encrypt = MD5(szImei + m_szDevIDShort);
//MD字符不为空,使用16位字符,为空,使用IMEI或设备信息拼接后的数据
if (TextUtils.isEmpty(md5Encrypt)) {
onlyOne = szImei == null ? m_szDevIDShort : szImei;
} else {
onlyOne = md5Encrypt;
}
return onlyOne;
}







/**
* 将给定的字符串MD5加密
*
* @param string 给定的字符串
* @return MD5加密后生成的字符串
*/
public static String MD5(String string) {
StringBuilder md5Str = new StringBuilder("");
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] b = md5.digest(string.getBytes("utf-8"));
int i;
for (byte aB : b) {
i = aB;
if (i < 0) i += 256;
if (i < 16) {
md5Str.append("0");
}
md5Str.append(Integer.toHexString(i));
}
} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
e.printStackTrace();
}
return md5Str.toString();
}
posted @ 2017-09-18 15:43  Android开发8585  阅读(533)  评论(0编辑  收藏