java android.provider.Settings.Secure.ANDROID_ID 返回“android_id”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26244512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:33:29  来源:igfitidea点击:

android.provider.Settings.Secure.ANDROID_ID returns "android_id"

javaandroid

提问by Fabii

Is there reason android.provider.Settings.Secure.ANDROID_ID is return the constant "android_id" instead of a 64-bit number as a hex string ?

android.provider.Settings.Secure.ANDROID_ID 是否有理由返回常量“android_id”而不是 64 位数字作为十六进制字符串?

Description : android.provider.Settings.Secure.ANDROID_ID

描述:android.provider.Settings.Secure.ANDROID_ID

  • A 64-bit number (as a hex string) that is randomly generated when the user first sets up the device and should remain constant for the lifetime of the user's device. The value may change if a factory reset is performed on the device.
  • 用户首次设置设备时随机生成的 64 位数字(作为十六进制字符串),并应在用户设备的整个生命周期内保持不变。如果在设备上执行恢复出厂设置,该值可能会更改。

I am using a Samsung Galaxy S4 w /

我正在使用三星 Galaxy S4 w /

 <uses-sdk android:minSdkVersion="13"  android:targetSdkVersion="19" />

Cheers

干杯

回答by Mike Laren

The android.provider.Settings.Secure.ANDROID_IDis a constant that can be used in android.provider.Settings.Secure.getString(ContentResolver resolver, String name). By default it is set to 'android_id' since that's the name of the property containing the actual Android ID.

android.provider.Settings.Secure.ANDROID_ID是,可以用在一个恒定android.provider.Settings.Secure.getString(ContentResolver resolver, String name)。默认情况下,它设置为“android_id”,因为这是包含实际 Android ID 的属性的名称。

Use this code to get the actual id:

使用此代码获取实际 ID:

String androidId = Secure.getString(getContext().getContentResolver(), Secure.ANDROID_ID); 

回答by RenniePet

This is just to elaborate on the answer by @MikeLaren, which is correct. But there are a couple of gotchas to be aware of, as documented in the following code, which is what I'm currently using:

这只是详细说明@MikeLaren 的答案,这是正确的。但是有几个问题需要注意,如以下代码中所述,这是我目前正在使用的:

  // 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;
      }
   }

回答by CrandellWS

android.provider.Settings.Secure.ANDROID_ID is to big so use this answer from: https://stackoverflow.com/a/10151694/1815624

android.provider.Settings.Secure.ANDROID_ID 太大,所以请使用以下答案:https: //stackoverflow.com/a/10151694/1815624

new BigInteger(string, 16).longValue()

For any value of someLong:

对于 someLong 的任何值:

new BigInteger(Long.toHexString(someLong), 16).longValue() == someLong

In other words, this will return the long you sent into Long.toHexString()for any long value, including negative numbers. It will also accept strings that are bigger than a long and silently return the lower 64 bits of the string as a long. You can just check the string length <= 16 (after trimming whitespace) if you need to be sure the input fits in a long.

换句话说,这将返回您发送Long.toHexString()的任何 long 值的 long,包括负数。它还将接受大于 long 的字符串,并默默地将字符串的低 64 位返回为 long。如果您需要确保输入适合很长,您可以只检查字符串长度 <= 16(修剪空格后)。

https://stackoverflow.com/a/10151694/1815624

https://stackoverflow.com/a/10151694/1815624