Android 唯一识别码

来源:互联网 发布:华夏名网数据库 编辑:程序博客网 时间:2024/05/16 04:57

本文转载自:http://blog.csdn.net/billpig/article/details/6728573

我们在项目过程中或多或少会使用到设备的唯一识别码,我们希望能够得到一个稳定、可靠的设备唯一识别码。今天我们将介绍几种方式。

       1. DEVICE_ID

假设我们确实需要用到真实设备的标识,可能就需要用到DEVICE_ID。在以前,我们的Android设备是手机,这个DEVICE_ID可以同通过TelephonyManager.getDeviceId()获取,它根据不同的手机设备返回IMEI,MEID或者ESN码,但它在使用的过程中会遇到很多问题:
  • 非手机设备: 如果只带有Wifi的设备或者音乐播放器没有通话的硬件功能的话就没有这个DEVICE_ID
  • 权限: 获取DEVICE_ID需要READ_PHONE_STATE权限,但如果我们只为了获取它,没有用到其他的通话功能,那这个权限有点大才小用
  • bug:在少数的一些手机设备上,该实现有漏洞,会返回垃圾,如:zeros或者asterisks的产品

        2. MAC ADDRESS

我们也可以通过手机的Wifi或者蓝牙设备获取MAC ADDRESS作为DEVICE ID,但是并不建议这么做,因为并不是所有的设备都有Wifi,并且,如果Wifi没有打开,那硬件设备无法返回MAC ADDRESS.

        3. Serial Number

在Android 2.3可以通过android.os.Build.SERIAL获取,非手机设备可以通过该接口获取。

        4. ANDROID_ID

ANDROID_ID是设备第一次启动时产生和存储的64bit的一个数,当设备被wipe后该数重置

ANDROID_ID似乎是获取Device ID的一个好选择,但它也有缺陷:

  • 它在Android <=2.1 or Android >=2.3的版本是可靠、稳定的,但在2.2的版本并不是100%可靠的
  • 在主流厂商生产的设备上,有一个很经常的bug,就是每个设备都会产生相同的ANDROID_ID:9774d56d682e549c

        5. Installtion ID : UUID

以上四种方式都有或多或少存在的一定的局限性或者bug,在这里,有另外一种方式解决,就是使用UUID,该方法无需访问设备的资源,也跟设备类型无关。

这种方式是通过在程序安装后第一次运行后生成一个ID实现的,但该方式跟设备唯一标识不一样,它会因为不同的应用程序而产生不同的ID,而不是设备唯一ID。因此经常用来标识在某个应用中的唯一ID(即Installtion ID),或者跟踪应用的安装数量。很幸运的,Google Developer Blog提供了这样的一个框架:

[java] view plaincopy
  1. public class Installation {  
  2.     private static String sID = null;  
  3.     private static final String INSTALLATION = "INSTALLATION";  
  4.   
  5.     public synchronized static String id(Context context) {  
  6.         if (sID == null) {    
  7.             File installation = new File(context.getFilesDir(), INSTALLATION);  
  8.             try {  
  9.                 if (!installation.exists())  
  10.                     writeInstallationFile(installation);  
  11.                 sID = readInstallationFile(installation);  
  12.             } catch (Exception e) {  
  13.                 throw new RuntimeException(e);  
  14.             }  
  15.         }  
  16.         return sID;  
  17.     }  
  18.   
  19.     private static String readInstallationFile(File installation) throws IOException {  
  20.         RandomAccessFile f = new RandomAccessFile(installation, "r");  
  21.         byte[] bytes = new byte[(int) f.length()];  
  22.         f.readFully(bytes);  
  23.         f.close();  
  24.         return new String(bytes);  
  25.     }  
  26.   
  27.     private static void writeInstallationFile(File installation) throws IOException {  
  28.         FileOutputStream out = new FileOutputStream(installation);  
  29.         String id = UUID.randomUUID().toString();  
  30.         out.write(id.getBytes());  
  31.         out.close();  
  32.     }  
  33. }  


           总结

综合以上所述,为了实现在设备上更通用的获取设备唯一标识,我们可以实现这样的一个类,为每个设备产生唯一的UUID,以ANDROID_ID为基础,在获取失败时以TelephonyManager.getDeviceId()为备选方法,如果再失败,使用UUID的生成策略。

重申下,以下方法是生成Device ID,在大多数情况下Installtion ID能够满足我们的需求,但是如果确实需要用到Device ID,那可以通过以下方式实现:

[java] view plaincopy
  1. import android.content.Context;  
  2. import android.content.SharedPreferences;  
  3. import android.provider.Settings.Secure;  
  4. import android.telephony.TelephonyManager;  
  5.   
  6. import java.io.UnsupportedEncodingException;  
  7. import java.util.UUID;  
  8.   
  9. public class DeviceUuidFactory {  
  10.     protected static final String PREFS_FILE = "device_id.xml";  
  11.     protected static final String PREFS_DEVICE_ID = "device_id";  
  12.   
  13.     protected static UUID uuid;  
  14.   
  15.   
  16.   
  17.     public DeviceUuidFactory(Context context) {  
  18.   
  19.         if( uuid ==null ) {  
  20.             synchronized (DeviceUuidFactory.class) {  
  21.                 if( uuid == null) {  
  22.                     final SharedPreferences prefs = context.getSharedPreferences( PREFS_FILE, 0);  
  23.                     final String id = prefs.getString(PREFS_DEVICE_ID, null );  
  24.   
  25.                     if (id != null) {  
  26.                         // Use the ids previously computed and stored in the prefs file  
  27.                         uuid = UUID.fromString(id);  
  28.   
  29.                     } else {  
  30.   
  31.                         final String androidId = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);  
  32.   
  33.                         // Use the Android ID unless it's broken, in which case fallback on deviceId,  
  34.                         // unless it's not available, then fallback on a random number which we store  
  35.                         // to a prefs file  
  36.                         try {  
  37.                             if (!"9774d56d682e549c".equals(androidId)) {  
  38.                                 uuid = UUID.nameUUIDFromBytes(androidId.getBytes("utf8"));  
  39.                             } else {  
  40.                                 final String deviceId = ((TelephonyManager) context.getSystemService( Context.TELEPHONY_SERVICE )).getDeviceId();  
  41.                                 uuid = deviceId!=null ? UUID.nameUUIDFromBytes(deviceId.getBytes("utf8")) : UUID.randomUUID();  
  42.                             }  
  43.                         } catch (UnsupportedEncodingException e) {  
  44.                             throw new RuntimeException(e);  
  45.                         }  
  46.   
  47.                         // Write the value out to the prefs file  
  48.                         prefs.edit().putString(PREFS_DEVICE_ID, uuid.toString() ).commit();  
  49.   
  50.                     }  
  51.   
  52.                 }  
  53.             }  
  54.         }  
  55.   
  56.     }  
  57.   
  58.   
  59.     /** 
  60.      * Returns a unique UUID for the current android device.  As with all UUIDs, this unique ID is "very highly likely" 
  61.      * to be unique across all Android devices.  Much more so than ANDROID_ID is. 
  62.      * 
  63.      * The UUID is generated by using ANDROID_ID as the base key if appropriate, falling back on 
  64.      * TelephonyManager.getDeviceID() if ANDROID_ID is known to be incorrect, and finally falling back 
  65.      * on a random UUID that's persisted to SharedPreferences if getDeviceID() does not return a 
  66.      * usable value. 
  67.      * 
  68.      * In some rare circumstances, this ID may change.  In particular, if the device is factory reset a new device ID 
  69.      * may be generated.  In addition, if a user upgrades their phone from certain buggy implementations of Android 2.2 
  70.      * to a newer, non-buggy version of Android, the device ID may change.  Or, if a user uninstalls your app on 
  71.      * a device that has neither a proper Android ID nor a Device ID, this ID may change on reinstallation. 
  72.      * 
  73.      * Note that if the code falls back on using TelephonyManager.getDeviceId(), the resulting ID will NOT 
  74.      * change after a factory reset.  Something to be aware of. 
  75.      * 
  76.      * Works around a bug in Android 2.2 for many devices when using ANDROID_ID directly. 
  77.      * 
  78.      * @see http://code.google.com/p/android/issues/detail?id=10603 
  79.      * 
  80.      * @return a UUID that may be used to uniquely identify your device for most purposes. 
  81.      */  
  82.     public UUID getDeviceUuid() {  
  83.         return uuid;  
  84.     }  
  85. }  

Reference:

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

http://stackoverflow.com/questions/5088474/how-can-i-get-the-uuid-of-my-android-phone-in-an-application


版权所有,转载请出明出处!

//////////////////////////////////////////////////////////////////////////////////////////////////////////

Java代码 复制代码 收藏代码
  1. String myIMSI = android.os.SystemProperties.get(android.telephony.TelephonyProperties.PROPERTY_IMSI);  
  2. // within my emulator it returns: 310995000000000 
  3.  
  4. String myIMEI = android.os.SystemProperties.get(android.telephony.TelephonyProperties.PROPERTY_IMEI);  
  5. // within my emulator it returns: 000000000000000 


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 手机系统占用内存大怎么办 头盔固定配件掉了怎么办 移动4g网络不好怎么办 wifi登录密码忘记了怎么办 电脑登录密码忘记了怎么办 笔记本电脑登录密码忘记了怎么办 信用卡登录密码忘记了怎么办 华为p9后置摄像头模糊怎么办 手机图片文件夹删了怎么办 发票系统导出的xml怎么办 微信支付被限制怎么办 跨行三天不到账怎么办 测速正常但实际很慢怎么办 善领wifi上网慢怎么办 文件太大拷贝不到u盘怎么办 电脑的视频文件不显示缩略图怎么办 无线网连接不上怎么办 路由器wifi密码忘了怎么办 路由器账号密码忘了怎么办 刷卡刷多了钱怎么办 刷卡刷了两次钱怎么办 小米手机强刷后一联网就被锁怎么办 苹果刷机出现的问题怎么办 小米手机解不开图案锁怎么办 小米6无限重启怎么办 小米5一直显示mi怎么办 小米手机电池进入休眠状态怎么办 小米3s开不开机怎么办 小米n充电关机开机不了怎么办? 关机后强制刷机怎么办 红米手机开机画面怎么办 红米note1无法清理数据怎么办 红米2a密码忘记怎么办 线刷也不成功该怎么办 红米2开不了机怎么办 魅族无限重启怎么办 坚果pro无法双清怎么办 usb外置网卡网速慢怎么办? 无线路由器被改密码怎么办 电脑打不开flv格式的视频怎么办 电脑打不开pdf格式的文件怎么办