Detailed explanation about AndroidQ can't get imsi solution
- 2021-11-13 18:15:38
- OfStack
Android Q prohibits obtaining IMEI and device serial number;
The official solution is as follows: https://developer.android.com/training/articles/user-data-ids
Combined with the official implementation scheme and actual needs, the implementation method is as follows:
Option 1:
/**
* Get device only 1 Identifier
*
* @return Only 1 Identifier
*/
public static String getDeviceId() {
// Pass SharedPreferences Get GUID
String guid = SPUtils.getInstance().getString(AppConfig.SP_GUID);
if (!TextUtils.isEmpty(guid)) {
return guid;
}
// Get ANDROID_ID
String android_id = Settings.System.getString(
App.getApp().getContentResolver(), Settings.Secure.ANDROID_ID);
if (!TextUtils.isEmpty(android_id)) {
// Pass ANDROID_ID Generate guid (Only 1 Identifier)
guid = EncryptUtils.encryptMD5ToString(android_id);
} else {
// Pass UUID Generate guid (Only 1 Identifier)
guid = EncryptUtils.encryptMD5ToString(UUID.randomUUID().toString());
}
// Save guid To SharedPreferences
SPUtils.getInstance().put(AppConfig.SP_GUID, guid);
return guid;
}
The above SPUtils is a tool class of SharedPreferences encapsulated by itself.
Option 2:
/**
* Get device only 1 Identifier
*
* @return Only 1 Identifier
*/
@SuppressLint("HardwareIds")
public static String getDeviceId() {
String m_szDevIDShort = "35" + 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 Bit
String serial = "serial";// Default serial Can be defined at will
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (ActivityCompat.checkSelfPermission(App.getApp(),
Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
// Due to Android Q Only 1 Changes in identifier permissions cause
// android.os.Build.getSerial() Return unknown,
// But m_szDevIDShort Is spelled out by hardware information, so it still guarantees UUID The only 1 Sex and persistence.
serial = android.os.Build.getSerial();// Android Q Returns from unknown
}
} else {
serial = Build.SERIAL;
}
} catch (Exception ignored) {
}
return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
}
Because m_szDevIDShort is spelled out by hardware information, the uniqueness and durability of UUID are guaranteed.