转自 Android技术分享
[链接:] (https://www.jianshu.com/p/d2df4c0c3420)
package com.hy.autolink.test002;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.telephony.TelephonyManager;
import java.security.MessageDigest;
import java.util.Locale;
import java.util.UUID;
import android.util.Log;
public class DeviceIdUtil {
/**
* 获得设备硬件标识
*
* @param context 上下文
* @return 设备硬件标识
*/
public String getDeviceId(Context context) {
StringBuilder sbDeviceId = new StringBuilder();
//获得设备默认IMEI(>=6.0 需要ReadPhoneState权限)
String imei = getIMEI(context);
Log.d("HY","IMEI: " +imei);
//获得AndroidId(无需权限)
String androidid = getAndroidId(context);
Log.d("HY","androidid: " +androidid);
//获得设备序列号(无需权限)
String serial = getSERIAL();
Log.d("HY","serial: " +serial);
//获得硬件uuid(根据硬件相关属性,生成uuid)(无需权限)
String uuid = getDeviceUUID().replace("-", "");
Log.d("HY","uuid: " +uuid);
//追加imei
if (imei != null && imei.length() > 0) {
sbDeviceId.append(imei);
sbDeviceId.append("|");
}
Log.d("HY","sbDeviceId 1: " +sbDeviceId);
//追加androidid
if (androidid != null && androidid.length() > 0) {
sbDeviceId.append(androidid);
sbDeviceId.append("|");
}
Log.d("HY","sbDeviceId 2: " +sbDeviceId);
//追加serial
if (serial != null && serial.length() > 0) {
sbDeviceId.append(serial);
sbDeviceId.append("|");
}
Log.d("HY","sbDeviceId 3: " +sbDeviceId);
//追加硬件uuid
if (uuid != null && uuid.length() > 0) {
sbDeviceId.append(uuid);
}
//生成SHA1,统一DeviceId长度
if (sbDeviceId.length() > 0) {
try {
byte[] hash = getHashByString(sbDeviceId.toString());
String sha1 = bytesToHex(hash);
if (sha1 != null && sha1.length() > 0) {
//返回最终的DeviceId
return sha1;
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
//如果以上硬件标识数据均无法获得,
//则DeviceId默认使用系统随机数,这样保证DeviceId不为空
return UUID.randomUUID().toString().replace("-", "");
}
//需要获得READ_PHONE_STATE权限,>=6.0,默认返回null
private String getIMEI(Context context) {
try {
TelephonyManager tm = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return "867931021606429";
}
return tm.getDeviceId();
} catch (Exception ex) {
ex.printStackTrace();
}
return "";
}
/**
* 获得设备的AndroidId
*
* @param context 上下文
* @return 设备的AndroidId
*/
private static String getAndroidId(Context context) {
try {
return Settings.Secure.getString(context.getContentResolver(),
Settings.Secure.ANDROID_ID);
} catch (Exception ex) {
ex.printStackTrace();
}
return "";
}
/**
* 获得设备序列号(如:WTK7N16923005607), 个别设备无法获取
*
* @return 设备序列号
*/
private static String getSERIAL() {
try {
return Build.SERIAL;
} catch (Exception ex) {
ex.printStackTrace();
}
return "";
}
/**
* 获得设备硬件uuid
* 使用硬件信息,计算出一个随机数
*
* @return 设备硬件uuid
*/
private static String getDeviceUUID() {
try {
String dev = "3883756" +
Build.BOARD.length() % 10 +
Build.BRAND.length() % 10 +
Build.DEVICE.length() % 10 +
Build.HARDWARE.length() % 10 +
Build.ID.length() % 10 +
Build.MODEL.length() % 10 +
Build.PRODUCT.length() % 10 +
Build.SERIAL.length() % 10;
return new UUID(dev.hashCode(),
Build.SERIAL.hashCode()).toString();
} catch (Exception ex) {
ex.printStackTrace();
return "";
}
}
/**
* 取SHA1
*
* @param data 数据
* @return 对应的hash值
*/
private static byte[] getHashByString(String data) {
try {
MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
messageDigest.reset();
messageDigest.update(data.getBytes("UTF-8"));
return messageDigest.digest();
} catch (Exception e) {
return "".getBytes();
}
}
/**
* 转16进制字符串
*
* @param data 数据
* @return 16进制字符串
*/
private static String bytesToHex(byte[] data) {
StringBuilder sb = new StringBuilder();
String stmp;
for (int n = 0; n < data.length; n++) {
stmp = (Integer.toHexString(data[n] & 0xFF));
if (stmp.length() == 1)
sb.append("0");
sb.append(stmp);
}
return sb.toString().toUpperCase(Locale.CHINA);
}
}
网友评论