獲取Android唯一標(biāo)識
開發(fā)中,進(jìn)程遇到需要獲取設(shè)備唯一標(biāo)識問題,有幾種方法可以作為參考
-
使用IMEI
但是僅僅對Android手機(jī)有效惰聂,并且添加權(quán)限: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 使用網(wǎng)卡地址
基本上的Android設(shè)備都配備WLAN帖鸦,可以通過WLAN地址來作為設(shè)備碼,同理劫拢,也需要加入android.permission.ACCESS_WIFI_STATE 權(quán)限肉津,否則返回null.
public synchronized static String getMacid(Context context) {
WifiManager wm = (WifiManager)getSystemService(Context.WIFI_SERVICE);
String WLANMAC = wm.getConnectionInfo().getMacAddress();
return WLANMAC ;
} ```
-
使用藍(lán)牙地址作為標(biāo)識
在有藍(lán)牙的設(shè)備上運(yùn)行。并且要加入android.permission.BLUETOOTH 權(quán)限.
public synchronized static String getMacid(Context context) {
BluetoothAdapter mBlueth= BluetoothAdapter.getDefaultAdapter();
String mBluethId= mBlueth.getAddress();
return mBluethId;
} ```
- ######**Installtion ID**
考慮到Android設(shè)備的多樣性舱沧,比如一些平板沒有通話功能妹沙,或者部分低價設(shè)備沒有WLAN或者藍(lán)牙,甚至用戶不愿意賦予APP這些需要的權(quán)限熟吏,我們就使用無需權(quán)限的方法;
這種方式的原理是在程序安裝后第一次運(yùn)行時生成一個ID距糖,該方式和設(shè)備唯一標(biāo)識不一樣玄窝,不同的應(yīng)用程序會產(chǎn)生不同的ID,同一個程序重新安裝也會不同悍引。所以這不是設(shè)備的唯一ID恩脂,但是可以保證每個用戶的ID是不同的∪そ铮可以說是用來標(biāo)識每一份應(yīng)用程序的唯一ID(即Installtion ID)俩块,可以用來跟蹤應(yīng)用的安裝數(shù)量等。
```/**
* 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**
綜上所述浓领,我們一共有五種方式取得設(shè)備的唯一標(biāo)識玉凯。它們中的一些可能會返回null,或者由于硬件缺失联贩、權(quán)限問題等獲取失敗壮啊。但你總能獲得至少一個能用。所以撑蒜,最好的方法就是通過拼接歹啼,或者拼接后的計算出的MD5值來產(chǎn)生一個結(jié)果。
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();```