这篇文章主要介绍了Android中IMEI怎么替换为Android_id的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Android中IMEI怎么替换为Android_id文章都会有所收获,下面我们一起来看看吧。
前置工作:
项目配置升到对应的29版本
compileSdkVersion: 29,
buildToolsVersion: ‘29.0.0',
minSdkVersion : 19,
targetSdkVersion : 29,
javaVersion : JavaVersion.VERSION_1_8
老版本获取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会重置。
如果用户拒绝权限,也还是会获取不到设备标识。
关于“Android中IMEI怎么替换为Android_id”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“Android中IMEI怎么替换为Android_id”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://www.xuebuyuan.com/3289446.html