The development process found a bit of an adaptive issue documented before Android has been using the AndroidUtilCode utility class when getting the device ID.

Implementation 'com. Blankj: utilcode: 1.30.6 / / if u use AndroidX, Use the following implementation 'com. Blankj: utilcodex: 1.30.6 PhoneUtils. GetDeviceId (this)Copy the code

The general method is as follows:

@SuppressLint("HardwareIds") @RequiresPermission(READ_PHONE_STATE) public static String getDeviceId() { TelephonyManager  tm = (TelephonyManager) Utils.getApp().getSystemService(Context.TELEPHONY_SERVICE); return tm ! = null ? tm.getDeviceId() : null; }Copy the code

The author carries out the operation of “return” for Android 10 and above users, but we need to adapt to this type of users in the development process, so we found the wrong description.

he user 10553 does not meet the requirements to access device identifiers.
Copy the code

As of Android 10 (API level 29), your app must be a device or profile owner app with a special carrier license. Or have the READ_PRIVILEGED_PHONE_STATE privilege to access the device identifiers that cannot be reset.

So we can use the official recommendation method, which is seen in the documentation, using SSAID, instance ids, AD ids, randomly generated ids, etc.

public final class PhoneUtil { private PhoneUtil() { throw new UnsupportedOperationException("u can't instantiate me..." ); } /** * Return the unique device id. * <p>If the version of SDK is greater than 28, it will return an empty string.</p> * <p>Must hold {@code <uses-permission android:name="android.permission.READ_PHONE_STATE" />}</p> * * @return the unique device id */ @SuppressLint("HardwareIds") @RequiresPermission(READ_PHONE_STATE) public static String getDeviceId(Context context) { if (Build.VERSION.SDK_INT >= 29) { return getUniqueID(context); } TelephonyManager tm = getTelephonyManager(); String deviceId = tm.getDeviceId(); if (! TextUtils.isEmpty(deviceId)) return deviceId; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { String imei = tm.getImei(); if (! TextUtils.isEmpty(imei)) return imei; String meid = tm.getMeid(); return TextUtils.isEmpty(meid) ? "" : meid; } return ""; } private static String getUniqueID(Context context) { String id = null; final String androidId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID); if (! TextUtils.isEmpty(androidId) && !" 9774d56d682e549c".equals(androidId)) { try { UUID uuid = UUID.nameUUIDFromBytes(androidId.getBytes("utf8")); id = uuid.toString(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } if (TextUtils.isEmpty(id)) { id = getUUID(); } return TextUtils.isEmpty(id) ? UUID.randomUUID().toString() : id; } private static String getUUID() { String serial = null; String m_szDevIDShort = "35" + Build.BOARD.length() % 10 + Build.BRAND.length() % 10 + ((null ! = Build.CPU_ABI) ? Build.CPU_ABI.length() : 0) % 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 bits if (build.version.sdk_int <= build.version_codes.p) {try {if (build.version.sdk_int >= build.version_codes.o) {//13 bits if (build.version.sdk_int <= build.version_codes.p) {  serial = android.os.Build.getSerial(); } else { serial = Build.SERIAL; } //API>=9 use serial number return new UUID(m_szDevidshor.hashCode (), serial.hashCode()).toString(); } catch (Exception exception) { serial = "serial" + UUID.randomUUID().toString(); }} else {serial = Android.os.build.unknown + uid.randomuuid ().tostring (); Return new UUID(m_szdevidshor.hashCode (), serial.hashcode ()).toString(); } private static TelephonyManager getTelephonyManager() { return (TelephonyManager) Utils.getApp().getSystemService(Context.TELEPHONY_SERVICE); }}Copy the code