安卓设备相对靠谱设备唯一码
2019-08-13 本文已影响18人
阿敏其人
public class DeviceIdUtils {
/**
* 获取唯一id
* @param context
* @return
*/
public static String getDeviceUUID(Context context) {
String uuid = loadDeviceUUID(context);
if (TextUtils.isEmpty(uuid)) {
uuid = buildDeviceUUID(context);
saveDeviceUUID(context, uuid);
}
return uuid;
}
private static String buildDeviceUUID(Context context) {
String androidId = getAndroidId(context);
if ("9774d56d682e549c".equals(androidId)) {
Random random = new Random();
androidId = Integer.toHexString(random.nextInt())
+ Integer.toHexString(random.nextInt())
+ Integer.toHexString(random.nextInt());
}
return new UUID(androidId.hashCode(), getBuildInfo().hashCode()).toString();
}
private static void saveDeviceUUID(Context context, String uuid) {
context.getSharedPreferences("device_uuid", Context.MODE_PRIVATE)
.edit()
.putString("uuid", uuid)
.apply();
}
@Nullable
private static String loadDeviceUUID(Context context) {
return context.getSharedPreferences("device_uuid", Context.MODE_PRIVATE)
.getString("uuid", null);
}
public static String getAndroidId(Context context) {
return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
}
public static String getBuildInfo() {
//这里选用了几个不会随系统更新而改变的值
StringBuffer buildSB = new StringBuffer();
buildSB.append(Build.BRAND).append("/");
buildSB.append(Build.PRODUCT).append("/");
buildSB.append(Build.DEVICE).append("/");
buildSB.append(Build.ID).append("/");
buildSB.append(Build.VERSION.INCREMENTAL);
return buildSB.toString();
// return Build.FINGERPRINT;
}
}
.
.
使用
DeviceIdUtils.getDeviceUUID(MainActivity.this)
.
.
此类非原创。