如何获取Android唯一标识
1个回答
2016-12-04 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
/**
* 获取android设备唯一标识
* @param context
* @return
*/
public static String getDeviceId(Context context){
TelephonyManager TelephonyMgr = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
String szImei = TelephonyMgr.getDeviceId();
Log.d(tag,"szImei = " + szImei);
String m_szDevIDShort = "35" + //we make this look like a valid IMEI
Build.BOARD.length()%10 +
Build.BRAND.length()%10 +
Build.CPU_ABI.length()%10 +
Build.DEVICE.length()%10 +
Build.DISPLAY.length()%10 +
Build.HOST.length()%10 +
Build.ID.length()%10 +
Build.MANUFACTURER.length()%10 +
Build.MODEL.length()%10 +
Build.PRODUCT.length()%10 +
Build.TAGS.length()%10 +
Build.TYPE.length()%10 +
Build.USER.length()%10 ; //13 digits
Log.d(tag,"m_szDevIDShort = " + m_szDevIDShort);
//String m_szAndroidID = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
//Log.d(tag,"m_szAndroidID = " + m_szAndroidID);
WifiManager wm = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
String m_szWLANMAC = wm.getConnectionInfo().getMacAddress();
Log.d(tag,"m_szWLANMAC = " + m_szWLANMAC);
BluetoothAdapter m_BluetoothAdapter = null; // Local Bluetooth adapter
m_BluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
String m_szBTMAC = m_BluetoothAdapter.getAddress();
Log.d(tag,"m_szBTMAC = " + m_szBTMAC);
String m_szLongID = szImei + "_" + m_szDevIDShort
+ "_" + m_szWLANMAC + "_" + m_szBTMAC;
// compute md5
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();
return m_szUniqueID;
}
* 获取android设备唯一标识
* @param context
* @return
*/
public static String getDeviceId(Context context){
TelephonyManager TelephonyMgr = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
String szImei = TelephonyMgr.getDeviceId();
Log.d(tag,"szImei = " + szImei);
String m_szDevIDShort = "35" + //we make this look like a valid IMEI
Build.BOARD.length()%10 +
Build.BRAND.length()%10 +
Build.CPU_ABI.length()%10 +
Build.DEVICE.length()%10 +
Build.DISPLAY.length()%10 +
Build.HOST.length()%10 +
Build.ID.length()%10 +
Build.MANUFACTURER.length()%10 +
Build.MODEL.length()%10 +
Build.PRODUCT.length()%10 +
Build.TAGS.length()%10 +
Build.TYPE.length()%10 +
Build.USER.length()%10 ; //13 digits
Log.d(tag,"m_szDevIDShort = " + m_szDevIDShort);
//String m_szAndroidID = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
//Log.d(tag,"m_szAndroidID = " + m_szAndroidID);
WifiManager wm = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
String m_szWLANMAC = wm.getConnectionInfo().getMacAddress();
Log.d(tag,"m_szWLANMAC = " + m_szWLANMAC);
BluetoothAdapter m_BluetoothAdapter = null; // Local Bluetooth adapter
m_BluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
String m_szBTMAC = m_BluetoothAdapter.getAddress();
Log.d(tag,"m_szBTMAC = " + m_szBTMAC);
String m_szLongID = szImei + "_" + m_szDevIDShort
+ "_" + m_szWLANMAC + "_" + m_szBTMAC;
// compute md5
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();
return m_szUniqueID;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询