3

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

Anil kumar
  • 51
  • 2
  • 10
  • 1
    See the following page, which explains which kind of identifiers should be used for which purposes in Android: https://developer.android.com/training/articles/user-data-ids – Peter O. Nov 05 '19 at 01:40
  • You can refer to my answer here. https://stackoverflow.com/questions/60501286/how-can-i-get-unique-device-id-in-android-10/63271485#63271485 – Abdallah AlTaher Sep 28 '20 at 11:55
  • Please refer to my answer at this link. https://stackoverflow.com/questions/60501286/how-can-i-get-unique-device-id-in-android-10/63271485#63271485 – Abdallah AlTaher Sep 28 '20 at 14:47

5 Answers5

3

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;
    }
Bodul
  • 185
  • 2
  • 12
Satish
  • 527
  • 6
  • 13
0

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


        }
    }
Jane Alam
  • 360
  • 2
  • 7
  • From Google's documentation: _"The advertising ID is a unique, user-resettable ID for advertising, provided by Google Play services."_ By "user-resettable ID," it means the ID can change by doing certain actions. – adrilz Jul 08 '20 at 06:24
0

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

0

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 e.g 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

Rahul_Pawar
  • 572
  • 1
  • 8
  • 16
0

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

val mId = Settings.Secure.getString(this.contentResolver, Settings.Secure.ANDROID_ID)
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 20 '22 at 06:08