zoukankan      html  css  js  c++  java
  • Android下获取设备唯一标识(UDID, DeviceID...)

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

    先附上完整代码

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



    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,然后保存起来。这个算是兼容方案,当然这样的设备并不会很多。

  • 相关阅读:
    SpringMvc执行流程
    Lock wait timeout exceeded; try restarting transaction解决方法
    MySQL删除复杂的重复数据的解决方案(一条数据项中包含多个值的情况)
    数据移植时递归运算查询部门及其下级所有部门的问题
    IDEA常用插件
    mybatis和mybatisPlus中解决实体类字段与数据库关键字冲突问题
    时间日期操作
    spring项目中使用MD5加密方式
    idea如何调出仪表盘
    scanf使用过程中的技巧与坑位
  • 原文地址:https://www.cnblogs.com/Nbox1989/p/4346347.html
Copyright © 2011-2022 走看看