如何获取“Android 10+”设备的唯一设备ID?

3

现在随着 Android 10 的权限和安全性更新,我们无法访问用户设备的设备 ID 和 IMEI 号码,但我需要一些设备的唯一标识来跟踪用户。

要求是我们希望每个电话只能登录一次/受限制。


1
请查看以下页面,该页面解释了在Android中应该使用哪种标识符以及其用途:https://developer.android.com/training/articles/user-data-ids - Peter O.
你可以参考我在这里的回答。https://dev59.com/8rnoa4cB1Zd3GeqPTJXS#63271485 - Abdallah AlTaher
请参考我在此链接中的回答。https://dev59.com/8rnoa4cB1Zd3GeqPTJXS#63271485 - Abdallah AlTaher
5个回答

3

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

这个在不同的应用程序中,在Android 10上会返回相同的值吗? - Arnold Brown
对于一些设备,当您重置设备时,设备的ID会更改。 - Satish
是的,在正常构建中,不同应用程序返回相同的ID。但是在从Playstore安装后,每个应用程序返回不同的ID。 - Arnold Brown
我强烈建议您查看此链接 https://android-developers.googleblog.com/2017/04/changes-to-device-identifiers-in.html。 - Satish
是的,我会检查这个。 - Arnold Brown
显示剩余4条评论

0

自API 26级别起,getDeviceId()已被弃用。

"READ_PRIVILEGE_PHONE_STATE"仅可由最佳实践建议访问,您应该“避免使用硬件标识符”作为唯一标识符。您可以使用来自Firebase的实例ID,例如FirebaseInstanceId.getInstance().getId();

 public static String getDeviceId(Context context) {

    String deviceId;

    if (android.os.Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
        deviceId = Settings.Secure.getString(
                context.getContentResolver(),
                Settings.Secure.ANDROID_ID);
    } else {
        deviceId =FirebaseInstanceId.getInstance().getId();
    }

    return deviceId;
}

在 Android 10 及以上版本中,请使用 FirebaseInstanceId.getInstance().getId();


0

这对我有效 //导入android.provider.Settings

val mId = Settings.Secure.getString(this.contentResolver, Settings.Secure.ANDROID_ID)

你的回答可以通过提供更多支持信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心中找到有关如何编写良好答案的更多信息。 - Community

0

0

Android引入了一个新的ID来唯一标识设备,称为Advertisement_Id。您可以从以下代码实现中获取此ID,在您的应用程序类onCreate方法中。

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

针对 Kotlin

     fun getAdId() {
        //Background Task
        AsyncTask.execute {
            var adInfo: AdvertisingIdClient.Info? = null
            try {
                adInfo = AdvertisingIdClient.getAdvertisingIdInfo(applicationContext)

                if(adInfo!=null){
                    val id = adInfo!!.getId()
                    val isLAT = adInfo!!.isLimitAdTrackingEnabled()

                    PersistData.setStringData(applicationContext, AppConstant.advertId, id)

                    val advertId = PersistData.getStringData(applicationContext, AppConstant.advertId)

                }

            } catch (e: IOException) {
                // Unrecoverable error connecting to Google Play services (e.g.,
                // the old version of the service doesn't support getting AdvertisingId).

            } catch (e: GooglePlayServicesAvailabilityException) {
                // Encountered a recoverable error connecting to Google Play services.

            } catch (e: GooglePlayServicesNotAvailableException) {
                // Google Play services is not available entirely.
            }


        }
    }

来自谷歌文档的内容:广告 ID 是由 Google Play 服务提供的唯一可重置的广告 ID。所谓“可重置的用户 ID”,是指该 ID 可以通过执行某些操作而更改。 - adrilz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接