简体   繁体   中英

Best possible way to get device Id in Android

I just wonder what would be the best to get device id I have 2 options either I use below code that would need run time permission android.permission.READ_PHONE_STATE -

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

Or

Secondly I have option of UUID that will not need any run time permission but need to be mapped with users data(like email or phone) to identify.

String uniqueID = UUID.randomUUID().toString();

Any suggestions which one to use and why would be appreciated.

Thanks

ANDROID ID:

On a device first boot, a random value is generated and stored. This value is available via Settings.Secure.ANDROID_ID. It's a 64-bit number that should remain constant for the lifetime of a device. ANDROID_ID seems a good choice for a unique device identifier because it's available for smartphones and tablets.

Issues: However, the value may change if a factory reset is performed on the device. There is also a known bug with a popular handset from a manufacturer where every instance has the same ANDROID_ID. Clearly, the solution is not 100% reliable.

UUID:

As the requirement for most applications is to identify a particular installation and not a physical device, a good solution to get a unique id for a user if to use the UUID class.

Issues: UUID.randomUUID() method generates a unique identifier for a specific installation. You have just to store that value and your user will be identified at the next launch of your application. You can also try to associate this solution with the Android Backup service to keep the information available for the user even if he installs your application on another device.

CONCLUSION:

Identify a particular device on Android is not an easy thing. There are many good reasons to avoid that. The best solution is probably to identify a particular installation by using the UUID solution. However, if you want absolutely identify a particular device physically, you can try to use the ANDROID_ID solution. Not 100% reliable but better than another solution.

you can use UUID class like this :

private static String uniqueID = null;
private static final String PREF_UNIQUE_ID = "PREF_UNIQUE_ID";
public synchronized static String id(Context context) {
    if (uniqueID == null) {
        SharedPreferences sharedPrefs = context.getSharedPreferences(
            PREF_UNIQUE_ID, Context.MODE_PRIVATE);
        uniqueID = sharedPrefs.getString(PREF_UNIQUE_ID, null);
    if (uniqueID == null) {
        uniqueID = UUID.randomUUID().toString();
        Editor editor = sharedPrefs.edit();
         editor.putString(PREF_UNIQUE_ID, uniqueID);
         editor.commit();
      }
   }    return uniqueID;
}

then take a look at here : https://www.pocketmagic.net/android-unique-device-id/ that Pseudo-Unique ID and Combined Device ID is not bad, maybe ;

ADROID_ID is the best way to get android device id it will be same until factory reset.

    public static String getDeviceId(Context context) {

    String id = Settings.Secure.getString(context.getContentResolver(),
            Settings.Secure.ANDROID_ID);
    return id;
}

To get Device ID you can use

fun getAndroidId(context: Context): String {
    val androidId =
        Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID)
    return androidId
}

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