Last active
February 19, 2019 08:57
-
-
Save beilly/b269f3112cbcf55c26697d3f68b2a42f to your computer and use it in GitHub Desktop.
实现在设备上更通用的获取设备唯一标识
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.benli.app.utils; | |
import android.content.Context; | |
import android.content.SharedPreferences; | |
import android.provider.Settings.Secure; | |
import android.telephony.TelephonyManager; | |
import android.util.Log; | |
import java.io.UnsupportedEncodingException; | |
import java.util.UUID; | |
/** | |
* @ClassName: DeviceUuidFactory | |
* @Description: | |
* 实现在设备上更通用的获取设备唯一标识(为每个设备产生唯一的UUID,以ANDROID_ID为基础,在获取失败时以TelephonyManager | |
* .getDeviceId()为备选方法,如果再失败,使用UUID的生成策略。 重申下,以下方法是生成Device | |
* ID,在大多数情况下Installtion ID能够满足我们的需求,但是如果确实需要用到Device ID) | |
* @date 2013-10-8 下午12:14:09 | |
* @Version 1.0 | |
*/ | |
public class DeviceUuidFactory { | |
protected static final String PREFS_FILE = "device_id"; | |
protected static final String PREFS_DEVICE_ID = "device_id"; | |
protected static UUID uuid; | |
private static String deviceType = "0"; | |
private static final String TYPE_ANDROID_ID = "1"; | |
private static final String TYPE_DEVICE_ID = "2"; | |
private static final String TYPE_RANDOM_UUID = "3"; | |
public DeviceUuidFactory(Context context) { | |
if (uuid == null) { | |
synchronized (DeviceUuidFactory.class) { | |
if (uuid == null) { | |
final SharedPreferences prefs = context.getSharedPreferences(PREFS_FILE, 0); | |
final String id = prefs.getString(PREFS_DEVICE_ID, null); | |
if (id != null) { | |
uuid = UUID.fromString(id); | |
} else { | |
final String androidId = Secure.getString(context.getContentResolver(), | |
Secure.ANDROID_ID); | |
try { | |
if (!"9774d56d682e549c".equals(androidId)) { | |
deviceType = TYPE_ANDROID_ID; | |
uuid = UUID.nameUUIDFromBytes(androidId.getBytes("utf8")); | |
} else { | |
final String deviceId = ((TelephonyManager) context | |
.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId(); | |
if (deviceId != null | |
&& !"0123456789abcdef".equals(deviceId.toLowerCase()) | |
&& !"000000000000000".equals(deviceId.toLowerCase())) { | |
deviceType = TYPE_DEVICE_ID; | |
uuid = UUID.nameUUIDFromBytes(deviceId.getBytes("utf8")); | |
} else { | |
deviceType = TYPE_RANDOM_UUID; | |
uuid = UUID.randomUUID(); | |
} | |
} | |
} catch (UnsupportedEncodingException e) { | |
deviceType = TYPE_RANDOM_UUID; | |
uuid = UUID.randomUUID(); | |
}finally { | |
uuid = UUID.fromString(deviceType + uuid.toString()); | |
} | |
prefs.edit().putString(PREFS_DEVICE_ID, uuid.toString()) | |
.commit(); | |
} | |
} | |
} | |
} | |
} | |
public UUID getDeviceUuid() { | |
Log.d("DeviceUuidFactory", "------>获取的设备ID号为:" + uuid.toString()); | |
return uuid; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment