获取Android唯一标识

开发中,进程遇到需要获取设备唯一标识问题,有几种方法可以作为参考

  • 使用IMEI
    但是仅仅对Android手机有效,并且添加权限:android.permission.READ_PHONE_STATE
    public synchronized static String getid(Context context) {    TelephonyManager TelephonyMgr =   (TelephonyManager)getSystemService(TELEPHONY_SERVICE);     String ID= TelephonyMgr.getDeviceId();    return ID;}
  • WLAN MAC Address 使用网卡地址
    基本上的Android设备都配备WLAN,可以通过WLAN地址来作为设备码,同理,也需要加入android.permission.ACCESS_WIFI_STATE 权限,否则返回null.
    public synchronized static String getMacid(Context context) {    WifiManager wm = (WifiManager)getSystemService(Context.WIFI_SERVICE);     String WLANMAC = wm.getConnectionInfo().getMacAddress();    return WLANMAC ; }
  • 使用蓝牙地址作为标识
    在有蓝牙的设备上运行。并且要加入android.permission.BLUETOOTH 权限.
    public synchronized static String getMacid(Context context) {     BluetoothAdapter mBlueth= BluetoothAdapter.getDefaultAdapter();      String mBluethId= mBlueth.getAddress();     return mBluethId; }
  • Installtion ID

    考虑到Android设备的多样性,比如一些平板没有通话功能,或者部分低价设备没有WLAN或者蓝牙,甚至用户不愿意赋予APP这些需要的权限,我们就使用无需权限的方法;
    这种方式的原理是在程序安装后第一次运行时生成一个ID,该方式和设备唯一标识不一样,不同的应用程序会产生不同的ID,同一个程序重新安装也会不同。所以这不是设备的唯一ID,但是可以保证每个用户的ID是不同的。可以说是用来标识每一份应用程序的唯一ID(即Installtion ID),可以用来跟踪应用的安装数量等。

    * Created by @dazhao on 2016/8/3 10:51.*/public class GetDeviceid {  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();  }}
  • Combined Device ID

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

    String m_szLongID = m_szImei + m_szDevIDShort   + m_szAndroidID+ m_szWLANMAC + m_szBTMAC;        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

更多相关文章

  1. android屏幕单位
  2. Android判断当前设备是否联网
  3. Android打开通讯录并获取数据
  4. 【Android】设备标识
  5. android--创建快捷方式和判断是否已经创建
  6. android系统学习笔记二
  7. Android设置权限问题
  8. Android(安卓)uses-feature
  9. Android(安卓)application 和 activity 标签详解

随机推荐

  1. Android中如何实现EditText的自动换行
  2. Android获取CPU架构
  3. Android 循环读取名字相似的图片
  4. Android解析json数据
  5. Android之判断当前网络状态
  6. 填坑
  7. android折叠展开自定义列表项测试
  8. 图片布局在android中资源文件夹中添加一
  9. Android 配置输出Apk名称和路径、渠道
  10. android:设置密码隐藏与显示