【问题标题】:Android unique id安卓唯一标识
【发布时间】:2010-06-25 06:07:38
【问题描述】:

如何从 Android 手机获取唯一 ID?

每当我尝试从手机中获取唯一 ID 作为字符串时 总是显示 android id 并且没有其他唯一的十六进制值。

我该怎么做 得到那个?

这是我到目前为止用来获取 ID 的代码:

String id=Settings.Secure.getString(contentResolver,Settings.Secure.ANDROID_ID);
Log.i("Android is is:",id);

我得到的输出如下所示:

Android id is: android id

我正在使用 Nexus One 进行测试。

【问题讨论】:

标签: android


【解决方案1】:

有关如何为安装您的应用程序的每台 Android 设备获取唯一标识符的详细说明,请参阅此官方 Android 开发者博客帖子:

http://android-developers.blogspot.com/2011/03/identifying-app-installations.html

似乎最好的方法是您在安装时自己生成一个,然后在重新启动应用程序时阅读它。

我个人认为这是可以接受的,但并不理想。 Android 提供的标识符在所有情况下都不起作用,因为大多数标识符取决于手机的无线电状态(wifi 开/关、蜂窝开/关、蓝牙开/关)。其他如 Settings.Secure.ANDROID_ID 必须由制造商实现,不保证唯一。

以下是将数据写入安装文件的示例,该文件将与应用程序在本地保存的任何其他数据一起存储。

public class Installation {
    private static String sID = null;
    private static final String INSTALLATION = "INSTALLATION";

    public synchronized static String id(Context context) {
        if (sID == null) {  
            File installation = new File(context.getFilesDir(), INSTALLATION);
            try {
                if (!installation.exists())
                    writeInstallationFile(installation);
                sID = readInstallationFile(installation);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return sID;
    }

    private static String readInstallationFile(File installation) throws IOException {
        RandomAccessFile f = new RandomAccessFile(installation, "r");
        byte[] bytes = new byte[(int) f.length()];
        f.readFully(bytes);
        f.close();
        return new String(bytes);
    }

    private static void writeInstallationFile(File installation) throws IOException {
        FileOutputStream out = new FileOutputStream(installation);
        String id = UUID.randomUUID().toString();
        out.write(id.getBytes());
        out.close();
    }
}

【讨论】:

    【解决方案2】:
    ((TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
    

    带有清单

    <uses-permission android:name='android.permission.READ_PHONE_STATE' />
    

    编辑:

    这里有一些关于 android id 的有趣读物:

    How to set the Android ID

    Android ID Requires Market Login

    尝试将其设置为“android id”以外的其他值,看看您是否读取了新值。

    【讨论】:

    • 感谢您的回复,但我想要 getDeviceID() 中的 32 位唯一 id,我只能得到 15 个字符,那么我怎样才能获得 android 设备的 32 位 HMAC MD5 id?
    • 请注意,此解决方案存在巨大限制:android-developers.blogspot.com/2011/03/…
    • 如果在没有手机的平板电脑上运行会怎样?
    • 它在 Nexus 7 平板电脑上返回空值
    【解决方案3】:

    这里是如何为您的 android 手机获取 androidId、唯一的 DeviceId 和序列号的代码段可能对您有所帮助。

    TelephonyManager tm = (TelephonyManager)getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
               final String DeviceId, SerialNum, androidId;
                DeviceId = tm.getDeviceId();
                SerialNum = tm.getSimSerialNumber();
                androidId = Secure.getString(getContentResolver(),Secure.ANDROID_ID);
    
                UUID deviceUuid = new UUID(androidId.hashCode(), ((long)DeviceId.hashCode() << 32) | SerialNum.hashCode());
                String mydeviceId = deviceUuid.toString();
                Log.v("My Id", "Android DeviceId is: " +DeviceId); 
                Log.v("My Id", "Android SerialNum is: " +SerialNum); 
                Log.v("My Id", "Android androidId is: " +androidId);  
    

    【讨论】:

      【解决方案4】:

      无线 MAC 地址比 IMEI 更独特,因为后者会在被盗设备上进行欺骗。缺点是它只适用于支持 WiFi 的设备。 WifiInfo

      【讨论】:

      • 请注意,此解决方案存在巨大限制:android-developers.blogspot.com/2011/03/…
      • 我测试过的许多手机 (30%) 在任何情况下都会返回 null 获取 Mac 地址。 部分手机只能在 Wifi 开启时报告。
      【解决方案5】:
      Settings.Secure.getString(contentResolver,Settings.Secure.ANDROID_ID);
      

      这不是一个好方法,它在某些情况下会返回 null。

      这段代码将帮助您生成唯一的伪设备 ID .......``

          public String getDeviceID() {
      
      /*String Return_DeviceID = USERNAME_and_PASSWORD.getString(DeviceID_key,"Guest");
      return Return_DeviceID;*/
      
      TelephonyManager TelephonyMgr = (TelephonyManager) getApplicationContext().getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
      String m_szImei = TelephonyMgr.getDeviceId(); // Requires
      // READ_PHONE_STATE
      
      // 2 compute DEVICE ID
      String m_szDevIDShort = "35"
      + // we make this look like a valid IMEI
      Build.BOARD.length() % 10 + Build.BRAND.length() % 10
      + Build.CPU_ABI.length() % 10 + Build.DEVICE.length() % 10
      + Build.DISPLAY.length() % 10 + Build.HOST.length() % 10
      + Build.ID.length() % 10 + Build.MANUFACTURER.length() % 10
      + Build.MODEL.length() % 10 + Build.PRODUCT.length() % 10
      + Build.TAGS.length() % 10 + Build.TYPE.length() % 10
      + Build.USER.length() % 10; // 13 digits
      // 3 android ID - unreliable
      String m_szAndroidID = Secure.getString(getContentResolver(),Secure.ANDROID_ID);
      // 4 wifi manager, read MAC address - requires
      // android.permission.ACCESS_WIFI_STATE or comes as null
      WifiManager wm = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
      String m_szWLANMAC = wm.getConnectionInfo().getMacAddress();
      // 5 Bluetooth MAC address android.permission.BLUETOOTH required
      BluetoothAdapter m_BluetoothAdapter = null; // Local Bluetooth adapter
      m_BluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
      String m_szBTMAC = m_BluetoothAdapter.getAddress();
      System.out.println("m_szBTMAC "+m_szBTMAC);
      
      // 6 SUM THE IDs
      String m_szLongID = m_szImei + m_szDevIDShort + m_szAndroidID+ m_szWLANMAC + m_szBTMAC;
      System.out.println("m_szLongID "+m_szLongID);
      MessageDigest m = null;
      try {
      m = MessageDigest.getInstance("MD5");
      } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
                      }
      m.update(m_szLongID.getBytes(), 0, m_szLongID.length());
      byte p_md5Data[] = m.digest();
      
      String m_szUniqueID = new String();
      for (int i = 0; i < p_md5Data.length; i++) {
      int b = (0xFF & p_md5Data[i]);
      // if it is a single digit, make sure it have 0 in front (proper
      // padding)
      if (b <= 0xF)
      m_szUniqueID += "0";
      // add number to string
      m_szUniqueID += Integer.toHexString(b);
      }
      m_szUniqueID = m_szUniqueID.toUpperCase();
      
      Log.i("-------------DeviceID------------", m_szUniqueID);
      Log.d("DeviceIdCheck", "DeviceId that generated MPreferenceActivity:"+m_szUniqueID);
      
      return m_szUniqueID;
      
      }
      

      【讨论】:

      • 这可以是唯一的,但使用起来并不安全,因为如果用户进行了出厂重置,Secure.ANDREID_ID 的值(可能)会更改,因此该方法将返回与之前不同的唯一 ID恢复出厂设置之前返回的那个。
      【解决方案6】:

      唯一标识

      Info adInfo = null;
      
       
      
      try {
      
           adInfo = AdvertisingIdClient.getAdvertisingIdInfo(mContext);
      
      } catch (IOException e) {
      
           ...
      
      } catch (GooglePlayServicesAvailabilityException e) {
      
           ...
      
      } catch (GooglePlayServicesNotAvailableException e) {
      
           ...
      
      }
      
       
      
      String AdId = adInfo.getId();
      

       

      【讨论】:

        猜你喜欢
        • 2013-11-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-05
        • 2015-05-08
        • 2012-06-02
        • 2012-11-21
        相关资源
        最近更新 更多