简体   繁体   English

Android 10 中没有适用于 Android 开发者的 IMEI

[英]No IMEI for Android Developers in Android 10

As Android is serious about security and trying to make new android versions more secure, its becoming tough for developers to keep up-to date with new security features and find old methods alternatives to make their app compatible with old features.由于 Android 非常重视安全性并试图使新的 android 版本更安全,因此开发人员很难跟上新的安全功能并找到旧方法的替代方案,使他们的应用程序与旧功能兼容。

This question is about IMEI in New Android 10!这个问题是关于新Android 10中的IMEI! The old method was super easy to get IMEI number by using below code使用以下代码,旧方法非常容易获取 IMEI 号码

  String deviceId = "";

    if (Build.VERSION.SDK_INT >= 26) {
        if (telMgr.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA) {
            deviceId = telMgr.getMeid();
        } else if (telMgr.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) {
            deviceId = telMgr.getImei();
        } else {
            deviceId = ""; // default!!!
        }
    } else {
        deviceId = telMgr.getDeviceId();
    }

In New Android 10 it is now restricted to get IMEI number.在新的 Android 10 中,现在限制获取 IMEI 号码。 According to android documentation根据android文档

apps must have the READ_PRIVILEGED_PHONE_STATE privileged permission in order to access the device's non-resettable identifiers, which include both IMEI and serial number.应用程序必须具有 READ_PRIVILEGED_PHONE_STATE 特权才能访问设备的不可重置标识符,包括 IMEI 和序列号。

The problem is that when we try to ask run time permissions with问题是当我们尝试询问运行时权限时

  android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE

My compiler does not recognize this permissions and i got error on this line but when i ask this permission in manifest file it does recognize this line but through warning that this permission is only for system apps.我的编译器无法识别此权限,并且我在此行上出错,但是当我在清单文件中询问此权限时,它确实识别了此行,但警告说此权限仅适用于系统应用程序。

I want to make my app compatible with Android 10 and want to get IMEI.我想让我的应用与 Android 10 兼容并想获得 IMEI。 How can i get IMEI number in Android 10 without becoming device owner or profile owner ?如何在不成为设备​​所有者或个人资料所有者的情况下在 Android 10 中获取 IMEI 号码?

Only device owner apps can read unique identifiers or your app must be a system app with READ_PRIVILEGED_PHONE_STATE .只有设备所有者应用才能读取唯一标识符,或者您的应用必须是具有READ_PRIVILEGED_PHONE_STATE的系统应用。 You can't ask this permission for a normal app.您不能为普通应用程序请求此权限。

Android 10 Restricted developer to Access IMEI number. Android 10 限制开发者访问 IMEI 号码。

You can have a alternate solution by get Software ID.您可以通过获取软件 ID 来获得替代解决方案。 You can use software id as a unique id.您可以使用软件 ID 作为唯一 ID。 Please find below code as i use in Application.请找到我在应用程序中使用的以下代码。

 public static String getDeviceId(Context context) { 

    String deviceId;

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

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

}else {

final TelephonyManager mTelephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);



     if (mTelephony.getDeviceId() != null) {
            deviceId = mTelephony.getDeviceId();
        } else {
            deviceId = Settings.Secure.getString(
                    context.getContentResolver(),
                    Settings.Secure.ANDROID_ID);
        }
    }

return deviceId;

}

To get a unique identifier you can use FirebaseInstanceId.getInstance().getId();要获得唯一标识符,您可以使用FirebaseInstanceId.getInstance().getId(); or String uniqueID = UUID.randomUUID().toString();String uniqueID = UUID.randomUUID().toString(); . . Have a look here for best practices https://developer.android.com/training/articles/user-data-ids#java在这里查看最佳实践https://developer.android.com/training/articles/user-data-ids#java

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM