【问题标题】:how to keep randomUUID static如何保持randomUUID静态
【发布时间】:2020-06-01 03:45:17
【问题描述】:

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

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

【问题讨论】:

  • 你应该把它保存到 SharedPreference 并检查它是否没有生成将创建新的并存储它
  • 不可能关闭正在运行的应用程序,因为此变量是静态的,但它属于正在运行的应用程序。你应该坚持
  • 您的意思不是“静态”,而是“持久”。已经是static
  • @CôngHải 谢谢你,它工作。

标签: java android static uuid


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2017-06-30
    • 1970-01-01
    • 2016-09-16
    • 1970-01-01
    • 1970-01-01
    • 2014-12-05
    • 2023-02-25
    • 1970-01-01
    • 2011-02-19
    相关资源
    最近更新 更多