java获取设备编号不一致_android唯一设备标识、设备号、设备ID的获取方法

本文探讨了在Android中获取设备唯一ID的方法,包括不同API级别下的解决方案。最佳方法是在API 9及以上版本使用Build类的属性和Android ID结合生成UUID,确保99.5%的设备具有唯一性。另一种方法是使用 SharedPreferences 存储UUID,但重装应用后会改变。第三种方法依赖于电话卡信息,需要相应权限。
摘要由CSDN通过智能技术生成

##如何获取Android设备唯一ID?

###问题 每一个android设备都有唯一ID吗?如果有?怎么用java最简单取得呢?

###回答1(最佳)

如何取得android唯一码?

好处:

1.不需要特定权限.

2.在99.5% Android装置(包括root过的)上,即API => 9,保证唯一性.

3.重装app之后仍能取得相同唯一值.

伪代码:

if API => 9/10: (99.5% of devices)

return unique ID containing serial id (rooted devices may be different)

else

return unique ID of build information (may overlap data - API < 9)

代码:

/*** Return pseudo unique ID

*@returnID*/@SuppressLint("NewApi")public staticString getUniquePsuedoID() {//If all else fails, if the user does have lower than API 9 (lower//than Gingerbread), has reset their device or 'Secure.ANDROID_ID'//returns 'null', then simply the ID returned will be solely based//off their Android device information. This is where the collisions//can happen.//Thankshttp://www.pocketmagic.net/?p=1662!//Try not to use DISPLAY, HOST or ID - these items could change.//If there are collisions, there will be overlapping data

String[] supportedABIArray;if (Build.VERSION.SDK_INT >= 21) {

supportedABIArray=Build.SUPPORTED_ABIS;

}else{

supportedABIArray= newString[] {Build.CPU_ABI};

}

String supportedABIs= "";try{for(String s : supportedABIArray) {

supportedABIs+=s;

}

}catch(Exception e) {

supportedABIs= "";

}

String m_szDevIDShort= "35";if (null != Build.BOARD) m_szDevIDShort += (Build.BOARD.length() % 10);if (null != Build.BRAND) m_szDevIDShort += (Build.BRAND.length() % 10);if (null != supportedABIs) m_szDevIDShort += (supportedABIs.length() % 10);if (null != Build.DEVICE) m_szDevIDShort += (Build.DEVICE.length() % 10);if (null != Build.MANUFACTURER) m_szDevIDShort += (Build.MANUFACTURER.length() % 10);if (null != Build.MODEL) m_szDevIDShort += (Build.MODEL.length() % 10);if (null != Build.PRODUCT) m_szDevIDShort += (Build.PRODUCT.length() % 10);//Thanks to @Roman SL!// http://stackoverflow.com/a/4789483/950427

//Only devices with API >= 9 have android.os.Build.SERIAL// http://developer.android.com/reference/android/os/Build.html#SERIAL//If a user upgrades software or roots their device, there will be a duplicate entry

String serial = null;try{

serial= android.os.Build.class.getField("SERIAL").get(null).toString();//Go ahead and return the serial for api => 9

return newUUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();

}catch(Exception exception) {//serial = "serial";//constant value

serial = "" + Calendar.getInstance().getTimeInMillis(); //variable value

}//Thanks @Joe!// http://stackoverflow.com/a/2853253/950427

//Finally, combine the values we have found by using the UUID class to create a unique//identifier

return newUUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();

}

###回答2 好处:

1.不需要特定权限.

2.在100% Android装置(包括root过的)上,保证唯一性.

坏处

1.重装app之后不能取得相同唯一值.

private static String uniqueID = null;private static final String PREF_UNIQUE_ID = "PREF_UNIQUE_ID";public synchronized staticString id(Context context) {if (uniqueID == null) {

SharedPreferences sharedPrefs=context.getSharedPreferences(

PREF_UNIQUE_ID, Context.MODE_PRIVATE);

uniqueID= sharedPrefs.getString(PREF_UNIQUE_ID, null);if (uniqueID == null) {

uniqueID=UUID.randomUUID().toString();

Editor editor=sharedPrefs.edit();

editor.putString(PREF_UNIQUE_ID, uniqueID);

editor.commit();

}

}returnuniqueID;

}

###回答3(需要有电话卡)

好处: 1.重装app之后仍能取得相同唯一值.

代码:

final TelephonyManager tm =(TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);finalString tmDevice, tmSerial, androidId;

tmDevice= "" +tm.getDeviceId();

tmSerial= "" +tm.getSimSerialNumber();

androidId= "" +android.provider.Settings.Secure.getString(getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);

UUID deviceUuid= new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32) |tmSerial.hashCode());

String deviceId= deviceUuid.toString();

谨记:要取得以下权限

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值