我使用Secure.Android_Id获取设备的唯一ID。但我需要将其作为服务的GUID传递给它。有没有简单的方法来创建它?
我写的基本代码看起来像 this :
String android_id = getAndroidId();
long leastSignificantBits = 0;
long mostSignificantBits = 0;
UUID uuid = null;
if (android_id != null) {
String asciiConvertedText = "";
int len = android_id.length();
for (int i = 0; i < len; i++) {
asciiConvertedText += "" + ((short) android_id.charAt(i));
}
len = asciiConvertedText.length();
if (len > 16) {
leastSignificantBits = Long.parseLong(asciiConvertedText.substring(0, 15));
if (asciiConvertedText.length() > 31) {
mostSignificantBits = Long.parseLong(asciiConvertedText.substring(16, 31));
} else {
mostSignificantBits = Long.parseLong(asciiConvertedText.substring(32,
asciiConvertedText.length()));
}
uuid = new UUID(mostSignificantBits, leastSignificantBits);
} else if (len > 0) {
leastSignificantBits = Long.parseLong(asciiConvertedText);
uuid = new UUID(mostSignificantBits, leastSignificantBits);
} else {
uuid = UUID.randomUUID();
}
}
if (uuid != null) {
deviceUUID = uuid.toString();
}
if (android_id != null) {
String asciiConvertedText = "";
int len = android_id.length();
for (int i = 0; i < len; i++) {
asciiConvertedText += "" + ((short) android_id.charAt(i));
}
len = asciiConvertedText.length();
if (len > 16) {
leastSignificantBits = Long.parseLong(asciiConvertedText.substring(0, 15));
if (asciiConvertedText.length() > 31) {
mostSignificantBits = Long.parseLong(asciiConvertedText.substring(16, 31));
} else {
mostSignificantBits = Long.parseLong(asciiConvertedText.substring(32,
asciiConvertedText.length()));
}
uuid = new UUID(mostSignificantBits, leastSignificantBits);
} else if (len > 0) {
leastSignificantBits = Long.parseLong(asciiConvertedText);
uuid = new UUID(mostSignificantBits, leastSignificantBits);
} else {
uuid = UUID.randomUUID();
}
}
if (uuid != null) {
deviceUUID = uuid.toString();
}
答案 0 :(得分:1)
new UUID(Secure.Android_Id.hashCode(), Secure.Android_Id.hashCode()
怎么样?
编辑:这是我的“唯一”ID代码:
public static String getDeviceId() {
final TelephonyManager tm = (TelephonyManager)Globals.Line2App.getSystemService(Context.TELEPHONY_SERVICE);
final String tmDevice, tmSerial, androidId;
tmDevice = "" + tm.getDeviceId();
tmSerial = "" + tm.getSimSerialNumber();
androidId = "" + Secure.getString(Globals.Line2App.getContentResolver(), Secure.ANDROID_ID);
UUID deviceUuid = new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32) | tmSerial.hashCode());
return deviceUuid.toString();
}
我不确定上面是否对你有所帮助,但是我使用了多个值的组合(我想我在stackoverflow上从另一个线程获得了这个代码)。