Android Q 适配开始
一、应用读取 Device ID
项目 targetSdkVersion为 29时 获取设备唯一标识时
SecurityException: getUniqueDeviceId: The user 10279 does not meet the requirements to access device identifiers.
在Android Q 之前有如下代码,获取设备Id,IMEI等
TelephonyManager telephonyManager =
(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String deviceID = telephonyManager.getDeviceId();
在Android Q之后以及改变
/**
* 获取设备号
* Android Q 无法再获取到 telephonyManager.getDeviceId()
* @return
*/
public static String getDivicesNumbr(Context context) {
String deviceID = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
if (TextUtils.isEmpty(deviceID)) {
deviceID = getUniquePsuedoID();
}
return deviceID;
}
/**
* 得到随机 UUID
*/
public static String getUUID() {
String uuid = SpUtils.getString("uuid", "");
if (TextUtils.isEmpty(uuid)) {
uuid = UUID.randomUUID().toString();
SpUtils.putString("uuid", uuid);
}
PLog.e("getDeviceId :", "getUUID : " + uuid);
return uuid;
}
public static String getUniquePsuedoID() {
String serial = null;
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 位
try {
serial = android.os.Build.class.getField("SERIAL").get(null).toString();
//API>=9 使用serial号
return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
} catch (Exception exception) {
//serial需要一个初始化
// 随便一个初始化
serial = "serial";
}
//使用硬件信息拼凑出来的15位号码
return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
}
二、Android Q手机在后台无法正常定位问题