Как получить уникальный идентификатор устройства для устройств "Android 10+"? - PullRequest
0 голосов
/ 04 ноября 2019

Теперь с обновленными правами и безопасностью для Android 10 мы не можем получить доступ к идентификатору устройства устройства пользователя и номеру IMEI, но мне нужен какой-то уникальный идентификатор устройства, чтобы мы могли отслеживать пользователя.

Требуется, чтобы мыхотите иметь / ограничить один логин с одного телефона

1 Ответ

0 голосов
/ 04 ноября 2019

Android представила новый идентификатор для уникальной идентификации устройства, который называется Advertising_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.
            }


        }
    }
...