Android 如何为“获取唯一的设备id”;安卓10+&引用;设备?

Android 如何为“获取唯一的设备id”;安卓10+&引用;设备?,android,android-security,Android,Android Security,现在android 10更新了权限和安全性,我们无法访问用户设备的设备id和IMEI号码,但我需要一些设备的唯一id,以便我们可以跟踪用户 要求是我们希望/限制一部手机的一次登录Android引入了一个新的id来唯一地识别一个设备,称为广告id。您可以从应用程序类onCreate方法中的以下代码实现中获取此Id /** Retrieve the Android Advertising Id * * The device must be KitKat (4.4)+

现在android 10更新了权限和安全性,我们无法访问用户设备的设备id和IMEI号码,但我需要一些设备的唯一id,以便我们可以跟踪用户


要求是我们希望/限制一部手机的一次登录

Android引入了一个新的id来唯一地识别一个设备,称为广告id。您可以从应用程序类onCreate方法中的以下代码实现中获取此Id

/** Retrieve the Android Advertising Id 
     * 
     * The device must be KitKat (4.4)+ 
     * This method must be invoked from a background thread.
     * 
     * */
    public static synchronized String getAdId (Context context) {

        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT) {
            return null;
        }

        AdvertisingIdClient.Info idInfo = null;
        try {
            idInfo = AdvertisingIdClient.getAdvertisingIdInfo(context);
        } catch (GooglePlayServicesNotAvailableException e) {
            e.printStackTrace();
        } catch (GooglePlayServicesRepairableException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String advertId = null;
        try{
            advertId = idInfo.getId();
        }catch (NullPointerException e){
            e.printStackTrace();
        }

        return advertId;
    }

Android 10限制开发者访问IMEI号码

您可以通过获取软件ID获得替代解决方案。您可以使用软件ID作为唯一ID。请查找我在应用程序中使用的以下代码

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

由于序列号和IMEI号已不推荐用于Android 10及更高版本, 因此,我们可以找到具有READ_PRIVILEGED_PHONE_状态的唯一标识符的android id

欲了解更多信息,请点击下面的链接。

请参见下一页,该页解释了在Android中应使用何种标识符用于何种目的:您可以在此处参考我的答案。请参考我在这个链接上的答案。这会通过不同的应用程序在安卓10上返回相同的值吗?我曾尝试使用设置获取安卓ID,它在正常构建上工作,但从play store安装时,不同应用程序上的值会不断变化。有什么想法吗?@ArnoldBrown是的,它将在不同的应用程序中返回相同的值。这是一个软件id。对于某些设备,当您重置设备时,id将更改。是的,它将在正常构建的不同应用程序上返回相同的id。但从Playstore安装后,每个应用程序都会从Google文档中返回不同的ID:“广告ID是一个唯一的、用户可重置的广告ID,由Google Play services提供。”通过“用户可重置ID”,这意味着该ID可以通过执行某些操作进行更改。
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;
    }