简体   繁体   中英

How to get a unique device id for " android 10+ " devices?

Now with android 10 updated permission and security, we cannot access the user's devices device id and IMEI number but I want some unique id of the device so that we can track the user.

The requirement is we want to have/restrict one login from one phone

Android 10 Restricted developer to Access IMEI number.

You can have a alternate solution by get Software ID. You can use software id as a unique 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;
    }

Android introduced a new id to identify a device uniquely, which is called Advertisement_Id . You can get this Id from the below code implementation in you Application class onCreate method.

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

For 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.
            }


        }
    }

As Serial number and IMEI number has been deprecated for Android 10 and onwards, So we can find the android id for unique identifier with READ_PRIVILEGED_PHONE_STATE.

for More information please follow below link. https://developer.android.com/training/articles/user-data-ids

getDeviceId() has been deprecated since API level 26.

"READ_PRIVILEGE_PHONE_STATE" is only accessible by The best practices suggest that you should "Avoid using hardware identifiers." for unique identifiers. You can use an instance id from firebase eg 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;
}

Use FirebaseInstanceId.getInstance().getId(); for Api level above Android 10

This works for me //import android.provider.Settings

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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