Android获取设备唯一标识

获取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<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();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值