现在的位置: 首页 > 综合 > 正文

android 手机设备唯一标识

2014年02月28日 ⁄ 综合 ⁄ 共 4296字 ⁄ 字号 评论关闭

 唯一标识码这东西在网络应用中非常有用,例如检测是否重复注册之类的。

Java代码 复制代码 收藏代码
  1. import android.provider.Settings.Secure; 
  2. private String
    android_id = Secure.getString(getContext().getContentResolver(), Secure.ANDROID_ID); 
  3.   
[java] view
plain
copy

  1. import android.provider.Settings.Secure;  
  2. private String android_id = Secure.getString(getContext().getContentResolver(), Secure.ANDROID_ID);  
  3.    

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

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

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

总结:

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

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

Java代码 复制代码 收藏代码
  1. import android.content.Context; 
  2. import android.content.SharedPreferences; 
  3. import android.provider.Settings.Secure; 
  4. import android.telephony.TelephonyManager; 
  5. import java.io.UnsupportedEncodingException; 
  6. import java.util.UUID; 
  7. public class DeviceUuidFactory
  8.     protected static final String
    PREFS_FILE = 
    "device_id.xml"
  9.     protected

抱歉!评论已关闭.