如何保持随机UUID静态



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

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

参考Công Hải的评论,我使用了SharedPpreferences,它工作得很好:

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

最新更新