1.API >=9:通过“Build.SERIAL”这个属性来保证ID的独一无二。
String m_szDevIDShort = "35" +
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 ; //13 位
try {
serial = android.os.Build.class.getField("SERIAL").get(null).toString();
//API>=9 使用serial号
return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
} catch (Exception exception) {
//serial需要一个初始化
serial = "serial"; // 随便一个初始化
}
//使用硬件信息拼凑出来的15位号码
return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
api<9
public static String getDeviceSerial() {
String serial = "unknown";
try {
Class clazz = Class.forName("android.os.Build");
Class paraTypes = Class.forName("java.lang.String");
Method method = clazz.getDeclaredMethod("getString", paraTypes);
if (!method.isAccessible()) {
method.setAccessible(true);
}
serial = (String)method.invoke(new Build(), "ro.serialno");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return serial;
}