public static String getDeviceId(Context context) {
final int targetSdkVersion = context.getApplicationInfo().targetSdkVersion;
if (targetSdkVersion > Build.VERSION_CODES.P && Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
return getUniqueID(context);
} else {
return getTelId(context);
}
}
private static String getTelId(Context context) {
final TelephonyManager manager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
return manager.getDeviceId();
}
private static String getUniqueID(Context context) {
String id = null;
final String androidId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
if (!TextUtils.isEmpty(androidId) && !"9774d56d682e549c".equals(androidId)) {
try {
UUID uuid = UUID.nameUUIDFromBytes(androidId.getBytes("utf8"));
id = uuid.toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
if (TextUtils.isEmpty(id)) {
id = getUUID();
}
return TextUtils.isEmpty(id) ? UUID.randomUUID().toString() : id;
}
private static String getUUID() {
String serial = null;
String m_szDevIDShort = "35" +
Build.BOARD.length() % 10 + Build.BRAND.length() % 10 +
((null != Build.CPU_ABI) ? Build.CPU_ABI.length() : 0) % 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; //13 位
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
serial = android.os.Build.getSerial();
} else {
serial = Build.SERIAL;
}
//API>=9 使用serial号
return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
} catch (Exception exception) {
serial = "serial" + UUID.randomUUID().toString(); // 随便一个初始化
}
} else {
serial = android.os.Build.UNKNOWN + UUID.randomUUID().toString(); // 随便一个初始化
}
//使用硬件信息拼凑出来的15位号码
return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
}