[英]how to keep randomUUID static
我生成了 UUID,但問題是當我關閉應用程序並再次運行它時,他會生成另一個 UUID,我只想生成一次 UUID,並在安裝應用程序后始終保持相同。
private final static String androidId = UUID.randomUUID().toString();
參考 Công Hải 的評論,我使用了 SharedPreference 並且效果很好:
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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.