0

I am developing a app in which i need to specify a user id to each user though it does not have any log in so i need to generate a user or device id so that i could understand to which device i need to send data . I tried using this but no result it is UUID.Unique iD

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();
            SharedPreferences.Editor editor = sharedPrefs.edit();
            editor.putString(PREF_UNIQUE_ID, uniqueID);
            editor.commit();
        }
    }
    return uniqueID;
}

Please help me out how to do it.

  • What do you mean by "no result"? I'm using `UUID.randomUUID().toString()` perfectly in my Android app, why will not this work for you? – fernandospr Feb 15 '19 at 19:42
  • Best practices for generating unique ids: https://developer.android.com/training/articles/user-data-ids?hl=en Notice `UUID.randomUUID().toString()` is one of them. – fernandospr Feb 15 '19 at 19:43
  • by no results i mean it is not working maybe i missing out something could you help me out how did you implemented it . – Tanishq Satsangi Feb 16 '19 at 05:03
  • What do you mean by "it is not working"? Have you debugged and checked the line `uniqueID = UUID.randomUUID().toString();` to see what value uniqueID has? – fernandospr Feb 16 '19 at 15:38

2 Answers2

1

You can use android device id which is unique :

private String android_id = Secure.getString(getContext().getContentResolver(), Secure.ANDROID_ID); 
ismail alaoui
  • 5,748
  • 2
  • 21
  • 38
0

you could try creating auto increment method. That way you won't get doubles.

C# Class Auto increment ID