android.provider.Settings.Secure.ANDROID_ID返回" android_id"

时间:2014-10-07 20:17:59

标签: java android

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

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

  • 64位数字(作为十六进制字符串),当时是随机生成的 用户首先设置设备,并应保持不变 用户设备的生命周期。如果是工厂,价值可能会改变 在设备上执行重置。

我正在使用三星Galaxy S4 w /

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

干杯

3 个答案:

答案 0 :(得分:19)

android.provider.Settings.Secure.ANDROID_ID是一个可以在android.provider.Settings.Secure.getString(ContentResolver resolver, String name)中使用的常量。默认情况下,它设置为&#39; android_id&#39;因为那是包含实际Android ID的属性的名称。

使用此代码获取实际ID:

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

答案 1 :(得分:3)

这只是为了详细说明@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;
      }
   }

答案 2 :(得分:0)

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

    [21:40:07.907][PowerShell     ][Info   ] Run script with parameters: -Create True -VhdPathOverride  -VhdSize 64424509440 -SwitchSubnetAddress 10.0.75.0 -SwitchSubnetMaskSize 24 -CPUs 2 -Memory 2048 -IsoFile C:\Program Files\Docker\Docker\Resources\docker-for-win.iso...
[21:40:07.907][HyperV         ][Info   ] Script started at 21:40:07.907
[21:40:07.907][HyperVGuids    ][Info   ] GUIDs installed
[21:40:07.929][HyperV         ][Info   ] Module loaded at 21:40:07.929
[21:40:07.929][Firewall       ][Info   ] All existing rules are removed.
[21:40:07.929][Firewall       ][Info   ] Opening ports for C:\Program Files\Docker\Docker\Resources\com.docker.proxy.exe...
[21:40:07.960][Firewall       ][Info   ] Opening ports for SMB...
[21:40:07.960][HyperV         ][Info   ] Creating Switch: DockerNAT...
[21:40:07.992][Firewall       ][Info   ] Ports are opened
[21:40:14.115][HyperV         ][Info   ] Switch created.
[21:40:23.431][HyperV         ][Info   ] Set IP address on switch
[21:40:24.387][HyperV         ][Info   ] Creating VM MobyLinuxVM...
[21:40:26.443][HyperV         ][Info   ] Setting CPUs to 2 and Memory to 2048 MB
[21:40:27.072][HyperV         ][Info   ] Attach VHD C:\Users\Public\Documents\Hyper-V\Virtual Hard Disks\MobyLinuxVM.vhdx

对于someLong的任何值:

new BigInteger(string, 16).longValue()

换句话说,这将返回您发送到new BigInteger(Long.toHexString(someLong), 16).longValue() == someLong 的任何长值,包括负数。它还将接受大于long的字符串,并以字符串的长度静默返回字符串的低64位。你可以检查字符串长度&lt; = 16(在修剪空格之后),如果你需要确保输入适合长。

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