温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android Q适配之IMEI替换为Android_id的示例分析

发布时间:2021-06-28 09:14:34 来源:亿速云 阅读:106 作者:小新 栏目:移动开发

这篇文章给大家分享的是有关Android Q适配之IMEI替换为Android_id的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

前置工作:

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

compileSdkVersion: 29,
buildToolsVersion: ‘29.0.0',
minSdkVersion : 19,
targetSdkVersion : 29,
javaVersion : JavaVersion.VERSION_1_8

Android Q适配之IMEI替换为Android_id的示例分析

升级到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会重置。

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

感谢各位的阅读!关于“Android Q适配之IMEI替换为Android_id的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI