简体   繁体   English

如何保持随机UUID static

[英]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.我生成了 UUID,但问题是当我关闭应用程序并再次运行它时,他会生成另一个 UUID,我只想生成一次 UUID,并在安装应用程序后始终保持相同。

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

reference to Công Hải's comment,I used SharedPreference and it work perfectly:参考 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM