android q适配工作,Android Q适配之IMEI替换为Android_id

前置工作:

项目配置升到对应的29版本

compileSdkVersion: 29,

buildToolsVersion: ‘29.0.0',

minSdkVersion : 19,

targetSdkVersion : 29,

javaVersion : JavaVersion.VERSION_1_8

36a886ffb2a0a2541afc13f78e74ec6e.png

升级到Android Q后的权限提示界面

老版本获取IMEI的方法:

public static String getIMEI(Context context) {

String deviceId = null;

try {

TelephonyManager tm = (TelephonyManager) context

.getSystemService(Context.TELEPHONY_SERVICE);

deviceId = tm.getDeviceId();

if (deviceId == null || "".equals(deviceId)) {

return getLocalMacAddress(context);

}

} catch (Exception e) {

e.printStackTrace();

if (deviceId == null || "".equals(deviceId)) {

return getLocalMacAddress(context);//获取Mac地址,在Android 9 P版本中,地址会随机变化,不可用作唯一标识,可去掉。

}

}

return deviceId;

}

Android Q获取IMEI方法

public static String getIMEI(Context context) {

String deviceId = null;

try {

TelephonyManager tm = (TelephonyManager) context

.getSystemService(Context.TELEPHONY_SERVICE);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {

deviceId = Settings.System.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);

} else {

// request old storage permission

if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {

// TODO: Consider calling

// ActivityCompat#requestPermissions

// here to request the missing permissions, and then overriding

// public void onRequestPermissionsResult(int requestCode, String[] permissions,

// int[] grantResults)

// to handle the case where the user grants the permission. See the documentation

// for ActivityCompat#requestPermissions for more details.

return null;

}

deviceId = tm.getDeviceId();

}

if (deviceId == null || "".equals(deviceId)) {

return getLocalMacAddress(context);

}

} catch (Exception e) {

e.printStackTrace();

if (deviceId == null || "".equals(deviceId)) {

return getLocalMacAddress(context);

}

}

return deviceId;

}

谷歌官方有声明:手机恢复出厂设置,Android ID会重置。

如果用户拒绝权限,也还是会获取不到设备标识。

所以具体优化需自行结合开发情景,有更好的建议的道友可以评论补充说明^ - ^…

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值