从thread开始,可以说Settings.Secure#ANDROID_ID
有时可能为空。另一方面,平板电脑设备中telephony-based ID
也可以为空,如果用户更改SIM卡或航班模式,则可以更改。{/ p>
考虑从这个thread获取mac地址,有时候mac地址也不能得到。
我是否有任何解决方案可以获得在任何情况下都不会改变的Android唯一ID?
答案 0 :(得分:0)
Rendy,我使用的代码就是上面的代码(来自问题Is there a unique Android device ID?的emmby回答(经过极少修改)):
public class DeviceUuidFactory {
protected static final String PREFS_FILE = "device_id.xml";
protected static final String PREFS_DEVICE_ID = "device_id";
protected volatile static UUID uuid;
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) {
// Use the ids previously computed and stored in the
// prefs file
uuid = UUID.fromString(id);
} else {
final String androidId = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
// Use the Android ID unless it's broken, in which case
// fallback on deviceId,
// unless it's not available, then fallback on a random
// number which we store
// to a prefs file
try {
if (!"9774d56d682e549c".equals(androidId)) {
uuid = UUID.nameUUIDFromBytes(androidId.getBytes("utf8"));
} else {
final String deviceId = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
uuid = (deviceId != null ? UUID.nameUUIDFromBytes(deviceId.getBytes("utf8")) : UUID.randomUUID());
}
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
// Write the value out to the prefs file
prefs.edit().putString(PREFS_DEVICE_ID, uuid.toString()).commit();
}
}
}
}
}
/**
* Returns a unique UUID for the current android device. As with all UUIDs,
* this unique ID is "very highly likely" to be unique across all Android
* devices. Much more so than ANDROID_ID is.
*
* The UUID is generated by using ANDROID_ID as the base key if appropriate,
* falling back on TelephonyManager.getDeviceID() if ANDROID_ID is known to
* be incorrect, and finally falling back on a random UUID that's persisted
* to SharedPreferences if getDeviceID() does not return a usable value.
*
* In some rare circumstances, this ID may change. In particular, if the
* device is factory reset a new device ID may be generated. In addition, if
* a user upgrades their phone from certain buggy implementations of Android
* 2.2 to a newer, non-buggy version of Android, the device ID may change.
* Or, if a user uninstalls your app on a device that has neither a proper
* Android ID nor a Device ID, this ID may change on reinstallation.
*
* Note that if the code falls back on using TelephonyManager.getDeviceId(),
* the resulting ID will NOT change after a factory reset. Something to be
* aware of.
*
* Works around a bug in Android 2.2 for many devices when using ANDROID_ID
* directly.
*
* @see http://code.google.com/p/android/issues/detail?id=10603
*
* @return a UUID that may be used to uniquely identify your device for most
* purposes.
*/
public UUID getDeviceUuid() {
return uuid;
}
}
要在活动中使用它,请执行以下操作:
UUID identifier = new DeviceUuidFactory(this).getDeviceUuid();
答案 1 :(得分:0)
您提到的选项几乎涵盖了获取唯一ID的所有不同方案,操作系统提供的唯一ID值已经被某些供应商错误地实施并返回相同的值对于该特定供应商的所有设备,所以不,没有1,只有最好的方法,通常你需要结合并验证是否存在一个或另一个,例如在我们的项目中:
首先我们选择TelephonyManager,如果它存在,我们从那里拿走
如果不是(大多数平板电脑都是如此),那么我们选择MAC地址
如果没有,则我们使用设置中提供的Android唯一ID。
希望它有所帮助!
问候!