Java 如何保持randomUUID的静态

Java 如何保持randomUUID的静态,java,android,static,uuid,Java,Android,Static,Uuid,我生成UUID,但问题是,当我关闭应用程序并再次运行它时,他会生成另一个UUID,我只想生成一次UUID,并在安装应用程序后保持不变 private final static String androidId = UUID.randomUUID().toString(); 提及科特迪瓦ả在我的评论中,我使用了SharedReference,它工作得非常完美: private static String uuid; SharedPreferences sPrefs = Preferenc

我生成UUID,但问题是,当我关闭应用程序并再次运行它时,他会生成另一个UUID,我只想生成一次UUID,并在安装应用程序后保持不变

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

提及科特迪瓦ả在我的评论中,我使用了SharedReference,它工作得非常完美:

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();
}

您应该将其保存到SharedReference,并检查它是否未生成将创建新的并存储它。如果您关闭应用程序正在运行,则不可能,因为此变量是静态的,但它属于正在运行的应用程序。你应该坚持你不是说“静态”,你是说“持续”。它已经是静态的了ả我感谢你,这很有效。