您的位置:首页 > 移动开发 > Android开发

Android获取设备唯一标识

2017-06-25 21:56 393 查看
获取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;
}


[b]Installtion ID[/b]
考虑到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 synchroni
4000
zed 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();
}
}


[b]Combined Device ID[/b]
综上所述,我们一共有五种方式取得设备的唯一标识。它们中的一些可能会返回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();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: