ANASS TAHIRI
ANASS TAHIRI

Reputation: 111

how to keep randomUUID static

I generate the UUID but the problem is that when I close the app and run it again he generate another UUID, I want to generate UUID just for one time, and keep it always the same after installing the application.

private final static String androidId = UUID.randomUUID().toString();

Upvotes: 1

Views: 480

Answers (1)

ANASS TAHIRI
ANASS TAHIRI

Reputation: 111

reference to Công Hải's comment ,I used SharedPreference and it work perfectly:

private static String uuid;  


SharedPreferences sPrefs = PreferenceManager.getDefaultSharedPreferences(this);
uuid = sPrefs.getString("uuid",null);
if (uuid == null){
      uuid = UUID.randomUUID().toString();
      SharedPreferences.Editor editor = sPrefs.edit();
      editor.putString("uuid",uuid);
      editor.apply();
}

Upvotes: 1

Related Questions