|
| 1 | +##如何取得android唯一码? |
| 2 | + |
| 3 | +###问题 |
| 4 | +每一个android装置都有唯一ID吗?如果有?怎么用java最简单取得呢? |
| 5 | + |
| 6 | +###回答1(最佳) |
| 7 | + |
| 8 | +如何取得android唯一码? |
| 9 | +好处: |
| 10 | +1.不需要特定权限. |
| 11 | +2.在99.5% Android装置(包括root过的)上,即API => 9,保证唯一性. |
| 12 | +3.重装app之后仍能取得相同唯一值. |
| 13 | + |
| 14 | +伪代码: |
| 15 | + |
| 16 | +``` |
| 17 | +if API => 9/10: (99.5% of devices) |
| 18 | +
|
| 19 | +return unique ID containing serial id (rooted devices may be different) |
| 20 | +
|
| 21 | +else |
| 22 | +
|
| 23 | +return unique ID of build information (may overlap data - API < 9) |
| 24 | +``` |
| 25 | + |
| 26 | +代码: |
| 27 | + |
| 28 | +```java |
| 29 | + |
| 30 | +/** |
| 31 | + * Return pseudo unique ID |
| 32 | + * @return ID |
| 33 | + */public static String getUniquePsuedoID() { |
| 34 | + // If all else fails, if the user does have lower than API 9 (lower |
| 35 | + // than Gingerbread), has reset their device or 'Secure.ANDROID_ID' |
| 36 | + // returns 'null', then simply the ID returned will be solely based |
| 37 | + // off their Android device information. This is where the collisions |
| 38 | + // can happen. |
| 39 | + // Thanks http://www.pocketmagic.net/?p=1662! |
| 40 | + // Try not to use DISPLAY, HOST or ID - these items could change. |
| 41 | + // If there are collisions, there will be overlapping data |
| 42 | + String m_szDevIDShort = "35" + (Build.BOARD.length() % 10) + (Build.BRAND.length() % 10) + (Build.CPU_ABI.length() % 10) + (Build.DEVICE.length() % 10) + (Build.MANUFACTURER.length() % 10) + (Build.MODEL.length() % 10) + (Build.PRODUCT.length() % 10); |
| 43 | + |
| 44 | + // Thanks to @Roman SL! |
| 45 | + // http://stackoverflow.com/a/4789483/950427 |
| 46 | + // Only devices with API >= 9 have android.os.Build.SERIAL |
| 47 | + // http://developer.android.com/reference/android/os/Build.html#SERIAL |
| 48 | + // If a user upgrades software or roots their device, there will be a duplicate entry |
| 49 | + String serial = null; |
| 50 | + try { |
| 51 | + serial = android.os.Build.class.getField("SERIAL").get(null).toString(); |
| 52 | + |
| 53 | + // Go ahead and return the serial for api => 9 |
| 54 | + return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString(); |
| 55 | + } catch (Exception exception) { |
| 56 | + // String needs to be initialized |
| 57 | + serial = "serial"; // some value |
| 58 | + } |
| 59 | + |
| 60 | + // Thanks @Joe! |
| 61 | + // http://stackoverflow.com/a/2853253/950427 |
| 62 | + // Finally, combine the values we have found by using the UUID class to create a unique identifier |
| 63 | + return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();} |
| 64 | +``` |
| 65 | +###回答2 |
| 66 | +好处: |
| 67 | +1.不需要特定权限. |
| 68 | +2.在100% Android装置(包括root过的)上,保证唯一性. |
| 69 | + |
| 70 | +坏处 |
| 71 | +1.重装app之后不能取得相同唯一值. |
| 72 | + |
| 73 | +```java |
| 74 | +private static String uniqueID = null; |
| 75 | +private static final String PREF_UNIQUE_ID = "PREF_UNIQUE_ID"; |
| 76 | + |
| 77 | +public synchronized static String id(Context context) { |
| 78 | + if (uniqueID == null) { |
| 79 | + SharedPreferences sharedPrefs = context.getSharedPreferences( |
| 80 | + PREF_UNIQUE_ID, Context.MODE_PRIVATE); |
| 81 | + uniqueID = sharedPrefs.getString(PREF_UNIQUE_ID, null); |
| 82 | + if (uniqueID == null) { |
| 83 | + uniqueID = UUID.randomUUID().toString(); |
| 84 | + Editor editor = sharedPrefs.edit(); |
| 85 | + editor.putString(PREF_UNIQUE_ID, uniqueID); |
| 86 | + editor.commit(); |
| 87 | + } |
| 88 | + } |
| 89 | + return uniqueID; |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +###回答3(需要有电话卡) |
| 94 | + |
| 95 | +好处: |
| 96 | +1.重装app之后仍能取得相同唯一值. |
| 97 | + |
| 98 | +代码: |
| 99 | + |
| 100 | +```java |
| 101 | + final TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE); |
| 102 | + final String tmDevice, tmSerial, androidId; |
| 103 | + tmDevice = "" + tm.getDeviceId(); |
| 104 | + tmSerial = "" + tm.getSimSerialNumber(); |
| 105 | + androidId = "" + android.provider.Settings.Secure.getString(getContentResolver(), android.provider.Settings.Secure.ANDROID_ID); |
| 106 | + UUID deviceUuid = new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32) | tmSerial.hashCode()); |
| 107 | + String deviceId = deviceUuid.toString(); |
| 108 | +``` |
| 109 | + |
| 110 | +谨记:要取得以下权限 |
| 111 | +``` |
| 112 | +<uses-permission android:name="android.permission.READ_PHONE_STATE" /> |
| 113 | +``` |
| 114 | +stackoverflow链接: |
| 115 | +http://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id |
0 commit comments