获取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),可以用来跟踪应用的安装数量等。

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 (b <= 0xF) {
                  m_szUniqueID+="0"; 
              }
              
          // add number to string  
          m_szUniqueID+=Integer.toHexString(b);
         }

 // hex string to uppercase 
 m_szUniqueID = m_szUniqueID.toUpperCase();
    原文作者:唠嗑008
    原文地址: https://www.jianshu.com/p/81570792e226
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞