为什么android.provider.Settings.Secure.ANDROID_ID返回常量"android_id“而不是64位数字作为十六进制字符串?
描述: android.provider.Settings.Secure.ANDROID_ID
我正在使用三星Galaxy S4 w/
<uses-sdk android:minSdkVersion="13" android:targetSdkVersion="19" />
干杯
发布于 2014-10-07 20:21:47
android.provider.Settings.Secure.ANDROID_ID
是一个常量,可以在android.provider.Settings.Secure.getString(ContentResolver resolver, String name)
中使用。默认情况下,它被设置为'android_id‘,因为这是包含实际android_id的属性的名称。
使用此代码获取实际id:
String androidId = Secure.getString(getContext().getContentResolver(), Secure.ANDROID_ID);
发布于 2015-09-16 20:49:12
这仅仅是为了详细说明@MikeL轮的答案,这是正确的。但是有几个问题需要注意,正如下面的代码所描述的,这就是我目前正在使用的:
// Get the unique (supposedly) ID for this Android device/user combination
long androidId = convertHexToLong(Settings.Secure.getString(
_applicationContext.getContentResolver(), Settings.Secure.ANDROID_ID));
……
// Method to convert a 16-character hex string into a Java long. This only took me about an hour,
// due to a known bug in Java that it took them 13 years to fix, and still isn't fixed in the
// version of Java used for Android development.
// http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4215269
// http://stackoverflow.com/questions/1410168/how-to-parse-negative-long-in-hex-in-java
// On top of that it turns out that on a HTC One the Settings.Secure.ANDROID_ID string may be 15
// digits instead of 16!
private long convertHexToLong(String hexString) {
hexString = "0000000000000000" + hexString;
int i = hexString.length();
hexString = hexString.substring(i - 16, i);
try {
return Long.parseLong(hexString.substring(0, 8), 16) << 32 |
Long.parseLong(hexString.substring(8, 16), 16);
} catch (Exception e) {
return 0L;
}
}
发布于 2018-05-10 02:55:19
android.provider.Settings.Secure.ANDROID_ID太大了,所以请使用以下答案:https://stackoverflow.com/a/10151694/1815624
new BigInteger(string, 16).longValue()
对于someLong的任何值:
new BigInteger(Long.toHexString(someLong), 16).longValue() == someLong
换句话说,这将返回任何长值(包括负数)发送到Long.toHexString()
的long。它还将接受大于一个长的字符串,并悄悄地将字符串的较低的64位作为long返回。您只需检查字符串长度<= 16 (在修整空格后),如果您需要确保输入符合一个长的。
https://stackoverflow.com/questions/26244512
复制相似问题