Replace IMEI of Android Q adapter with Android_ id

Front work:

Upgrade the project configuration to the corresponding version 29

Permission prompt interface after upgrading to Android Q

Method of obtaining IMEI in old version:

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 get IMEI method

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;
  }

Google's official statement: the Android ID will be reset when the phone is restored to the factory settings.

If the user denies permission, the device ID will still not be obtained.

Therefore, the specific optimization needs to be combined with the development scenario. Taoist friends with better suggestions can comment and supplement ^ - ^

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>