How to get Device unique Id in my android application?

android.telephony.TelephonyManager.getDeviceId() is not working after migrating to API level 29 (Android 10), as it's throwing SecurityException. Please anyone can help out me to get a unique device id.

Upvotes: 0

Views: 12995

Answers (6)

vijay kumar
vijay kumar

Reputation: 26

If your project contains firebase. use this --> FirebaseInstanceId.getInstance().getToken()

Upvotes: 0

Sandeep Malik
Sandeep Malik

Reputation: 1976

set this permission in manifeast:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

Now use this to get unique no:

TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
  telephonyManager.getDeviceId();

Upvotes: 0

Ezaldeen sahb
Ezaldeen sahb

Reputation: 739

getDeviceId is deprecated you can use this method to get the device IMEI

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        IMEINumber = telephonyMgr.getImei();
    } else {
        IMEINumber = telephonyMgr.getDeviceId();
    }

Upvotes: -1

Vir Rajpurohit
Vir Rajpurohit

Reputation: 1937

You can use below which is preferrable which has the least chances of reset.

private String android_id = Secure.getString(getContext().getContentResolver(),
                                                        Secure.ANDROID_ID); 

Please refer THIS from which you can decide the preferable one.

Upvotes: 1

Prajwal Waingankar
Prajwal Waingankar

Reputation: 2710

Use the below code:

 public String androidId;

 androidId = String
                .format("%16s", Settings.Secure.getString(getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID))
                .replace(' ', '0');

Upvotes: 1

Arsal Imam
Arsal Imam

Reputation: 2992

As per the latest release in Android 10, Restriction on non-resettable device identifiers. PPS must have the READ_PRIVILEGED_PHONE_STATE privileged permission in order to access the device's non-resettable identifiers, which include both IMEI and serial number.

"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();.

Or you can go with this also,

String deviceId = android.provider.Settings.Secure.getString(
                    context.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);

Upvotes: 3

Related Questions