• 如何获取Android手机的唯一标识


    有很多场景和需求你需要用到手机设备的唯一标识符。

    Android中,有以下几种方法获取这样的ID。

    1. The IMEI: 仅仅只对Android手机有效:

    1
    2
    TelephonyManager TelephonyMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE); 
    String szImei = TelephonyMgr.getDeviceId(); 
    

    采用此种方法,需要在AndroidManifest.xml中加入一个许可:android.permission.READ_PHONE_STATE,并且用户应当允许安装此应用。作为手机来讲,IMEI是唯一的,它应该类似于 359881030314356(除非你有一个没有量产的手机(水货)它可能有无效的IMEI,如:0000000000000)。

    2. Pseudo-Unique ID, 这个在任何Android手机中都有效
    有一些特殊的情况,一些如平板电脑的设置没有通话功能,或者你不愿加入READ_PHONE_STATE许可。而你仍然想获得唯一序列号之类的东西。这时你可以通过取出ROM版本、制造商、CPU型号、以及其他硬件信息来实现这一点。这样计算出来的ID不是唯一的(因为如果两个手机应用了同样的硬件以及Rom 镜像)。但应当明白的是,出现类似情况的可能性基本可以忽略。要实现这一点,你可以使用Build类:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    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
    

    大多数的Build成员都是字符串形式的,我们只取他们的长度信息。我们取到13个数字,并在前面加上“35”。这样这个ID看起来就和15位IMEI一样了。

    3. The Android ID
    通常被认为不可信,因为它有时为null。开发文档中说明了:这个ID会改变如果进行了出厂设置。并且,如果某个Andorid手机被Root过的话,这个ID也可以被任意改变。

    1
    String m_szAndroidID = Secure.getString(getContentResolver(), Secure.ANDROID_ID);
    

    Returns: 9774d56d682e549c . 无需任何许可。

    4. The WLAN MAC Address string
    是另一个唯一ID。但是你需要为你的工程加入android.permission.ACCESS_WIFI_STATE 权限,否则这个地址会为null。

    1
    2
    WifiManager wm = (WifiManager)getSystemService(Context.WIFI_SERVICE); 
    String m_szWLANMAC = wm.getConnectionInfo().getMacAddress();
    

    Returns: 00:11:22:33:44:55 (这不是一个真实的地址。而且这个地址能轻易地被伪造。).WLan不必打开,就可读取些值。

    5. The BT MAC Address string
    只在有蓝牙的设备上运行。并且要加入android.permission.BLUETOOTH 权限.

    1
    2
    3
    BluetoothAdapter m_BluetoothAdapter = null; // Local Bluetooth adapter      
    m_BluetoothAdapter = BluetoothAdapter.getDefaultAdapter();      
    String m_szBTMAC = m_BluetoothAdapter.getAddress();
    

    Returns: 43:25:78:50:93:38 . 蓝牙没有必要打开,也能读取。

    Combined Device ID
    综上所述,我们一共有五种方式取得设备的唯一标识。它们中的一些可能会返回null,或者由于硬件缺失、权限问题等获取失败。
    但你总能获得至少一个能用。所以,最好的方法就是通过拼接,或者拼接后的计算出的MD5值来产生一个结果。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    String m_szLongID = m_szImei + m_szDevIDShort 
        + m_szAndroidID+ m_szWLANMAC + m_szBTMAC;      
    // compute md5     
     MessageDigest m = null;   
    try {
     m = MessageDigest.getInstance("MD5");
     } catch (NoSuchAlgorithmException e) {
     e.printStackTrace();   
    }    
    m.update(m_szLongID.getBytes(),0,m_szLongID.length());   
    // get md5 bytes   
    byte p_md5Data[] = m.digest();   
    // create a hex string   
    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); 
       }   // hex string to uppercase   
    m_szUniqueID = m_szUniqueID.toUpperCase();
    

    通过以上算法,可产生32位的16进制数据:
    9DDDF85AFF0A87974CE4541BD94D5F55
    现在你就可以对其进行你的应用了。

     

     

     

     

    //---------------------------------------------------------------------

    android下获取设备唯一标识原本非常简单(至少不会像iOS一样禁用这个,禁用那个),但是由于设备的多样性需要考虑的东西也对应复杂起来。
      先附上完整代码
      java代码

    1.     protected static final String PREFS_FILE = 
    2. "gank_device_id.xml";
    3.   protected static final String PREFS_DEVICE_ID = "gank_device_id";
    4.   protected static String uuid;
    5.   static public String getUDID()
    6.   {
    7.   if( uuid ==null ) {
    8.   synchronized (GankMainActivity.class) {
    9.   if( uuid == null) {
    10.   final SharedPreferences prefs = s_instance.getSharedPreferences( 
    11. PREFS_FILE, 0);
    12.   final String id = prefs.getString(PREFS_DEVICE_ID, null );
    13.   if (id != null) {
    14.   // Use the ids previously computed and stored in the prefs file
    15.   uuid = id;
    16.   } else {
    17.   final String androidId = Secure.getString(s_instance.getContentResolver(), 
    18. Secure.ANDROID_ID);
    19.   // Use the Android ID unless it's broken, in which case fallback on 
    20. deviceId,
    21.   // unless it's not available, then fallback on a random number which we 
    22. store
    23.   // to a prefs file
    24.   try {
    25.   if (!"9774d56d682e549c".equals(androidId)) {
    26.   uuid = UUID.nameUUIDFromBytes(androidId.getBytes("utf8")).toString();
    27.   } else {
    28.   final String deviceId = ((TelephonyManager) s_instance.getSystemService( 
    29. Context.TELEPHONY_SERVICE )).getDeviceId();
    30.   uuid = deviceId!=null ? 
    31. UUID.nameUUIDFromBytes(deviceId.getBytes("utf8")).toString() : 
    32. UUID.randomUUID().toString();
    33.   }
    34.   } catch (UnsupportedEncodingException e) {
    35.   throw new RuntimeException(e);
    36.   }
    37.   // Write the value out to the prefs file
    38.   prefs.edit().putString(PREFS_DEVICE_ID, uuid).commit();
    39.   }
    40.   }
    41.   }
    42.   }
    43.   return uuid;
    44.   }
    复制代码


      1、正常情况下可以通过((TelephonyManager) s_instance.getSystemService( Context.TELEPHONY_SERVICE )).getDeviceId(); 来获取,但是某些平板电脑此函数会返回空
      2、通过 Secure.getString(s_instance.getContentResolver(), Secure.ANDROID_ID); 也可以获取到一个id,但是android2.2或者是某些山寨手机使用这个也是有问题的,它会返回一个固定的值 9774d56d682e549c
      3、如果前两个都没有获取到udid,那么就在程序启动的时候创建一个随机的uuid,然后保存起来。这个算是兼容方案,当然这样的设备并不会很多。


      原文链接:http://www.eyeandroid.com/thread-15908-1-1.html

  • 相关阅读:
    ef左联三张表案例
    sql常用语句汇总
    最初的ajax案例
    asp.net mvc请求响应模型原理回顾
    viewstate cookie和session原理回顾
    一般处理程序装配数据到html页的原理
    asp.net页面生命周期回顾
    rocketMQ 消息的 tag
    rocketMQ 订阅关系
    rocketMQ retry 消息的实现
  • 原文地址:https://www.cnblogs.com/changyiqiang/p/6378642.html
Copyright © 2020-2023  润新知