日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

android唯一设备标识、设备号、设备ID的获取方法

發(fā)布時(shí)間:2025/3/18 编程问答 65 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android唯一设备标识、设备号、设备ID的获取方法 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

##如何獲取Android設(shè)備唯一ID?

###問(wèn)題 每一個(gè)android設(shè)備都有唯一ID嗎?如果有?怎么用java最簡(jiǎn)單取得呢?

###回答1(最佳)

如何取得android唯一碼?

好處:

  • 1.不需要特定權(quán)限.
  • 2.在99.5% Android裝置(包括root過(guò)的)上,即API => 9,保證唯一性.
  • 3.重裝app之后仍能取得相同唯一值.

偽代碼:

if API => 9/10: (99.5% of devices)return unique ID containing serial id (rooted devices may be different)elsereturn unique ID of build information (may overlap data - API < 9)

代碼:

/*** Return pseudo unique ID* @return ID*/ @SuppressLint("NewApi") public static String 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.// Thanks http://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 = new String[] {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 entryString serial = null;try {serial = android.os.Build.class.getField("SERIAL").get(null).toString();// Go ahead and return the serial for api => 9return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();} catch (Exception exception) {// serial = "serial"; // constant valueserial = "" + 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// identifierreturn new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString(); }

?

###回答2 好處:

  • 1.不需要特定權(quán)限.
  • 2.在100% Android裝置(包括root過(guò)的)上,保證唯一性.

壞處

  • 1.重裝app之后不能取得相同唯一值.
private static String uniqueID = null; private static final String PREF_UNIQUE_ID = "PREF_UNIQUE_ID";public synchronized static String 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();}}return uniqueID; }

###回答3(需要有電話卡)

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

代碼:

final TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);final String 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();

謹(jǐn)記:要取得以下權(quán)限

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

?

總結(jié)

以上是生活随笔為你收集整理的android唯一设备标识、设备号、设备ID的获取方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。