Android Q适配之IMEI替换为Android_id
作者:广岛冰盐 时间:2022-08-08 00:46:01
前置工作:
项目配置升到对应的29版本
compileSdkVersion: 29,
buildToolsVersion: ‘29.0.0',
minSdkVersion : 19,
targetSdkVersion : 29,
javaVersion : JavaVersion.VERSION_1_8
升级到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会重置。
如果用户拒绝权限,也还是会获取不到设备标识。
所以具体优化需自行结合开 * 景,有更好的建议的道友可以评论补充说明^ - ^…
来源:https://blog.csdn.net/qq_23432027/article/details/99305996
标签:AndroidQ,IMEI,Android,id
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android自定义view仿淘宝快递物流信息时间轴
2022-02-20 18:51:16
![](https://img.aspxhome.com/file/2023/7/139387_0s.jpg)
总结C#删除字符串数组中空字符串的几种方法
2022-04-14 03:26:42
![](https://img.aspxhome.com/file/2023/9/129679_0s.jpg)
SpringBoot自动配置原理详解
2023-08-19 09:25:55
![](https://img.aspxhome.com/file/2023/6/58096_0s.jpg)
Java中简单实用Quartz概述
2021-09-09 14:16:30
C#中abstract的用法详解
2021-07-08 21:53:46
Java中的守护线程问题
2023-09-13 11:49:23
![](https://img.aspxhome.com/file/2023/7/75427_0s.png)
Spring配置中transactionAttributes的使用方法介绍
2021-06-16 10:59:05
关于jdk9、jdk10、jdk11、jdk12、jdk13新特性说明
2021-07-19 17:06:34
![](https://img.aspxhome.com/file/2023/5/85415_0s.png)
Android仿微信菜单(Menu)(使用C#和Java分别实现)
2023-02-13 18:22:11
![](https://img.aspxhome.com/file/2023/3/111673_0s.gif)
Java中常用的设计模式之观察者模式详解
2023-07-27 12:32:52
![](https://img.aspxhome.com/file/2023/0/75450_0s.png)
java中Date类和Strng类的灵活转化
2022-12-09 07:47:24
Android实战教程第九篇之短信高效备份
2022-02-14 12:59:20
Android UI设计之AlertDialog弹窗控件
2023-05-25 07:50:14
![](https://img.aspxhome.com/file/2023/8/137618_0s.gif)
浅谈MyBatis 事务管理
2022-03-22 16:17:11
Android学习笔记(一)环境安装及第一个hello world
2022-03-10 03:05:43
![](https://img.aspxhome.com/file/2023/2/139272_0s.png)
Spring Boot深入排查 java.lang.ArrayStoreException异常
2023-07-11 16:31:27
RollViewPager图片轮播效果开源框架使用方法详解
2021-12-06 07:22:59
![](https://img.aspxhome.com/file/2023/8/139398_0s.gif)
以Java代码的方式总结几个典型的内存溢出案例
2023-06-11 04:00:56
![](https://img.aspxhome.com/file/2023/9/129179_0s.png)
Spring Cloud动态配置刷新RefreshScope使用示例详解
2022-05-23 15:05:32
C语言 socketpair用法案例讲解
2022-07-24 12:41:48