【发布时间】:2019-07-09 23:07:11
【问题描述】:
我正在开发一个应用程序,我需要为每个用户指定一个用户 ID,尽管它没有任何登录,所以我需要生成一个用户或设备 ID,以便我可以了解我需要发送到哪个设备数据 。 我尝试使用它但没有结果它是 UUID.Unique iD
private static String uniqueID = null;
private static final String PREF_UNIQUE_ID = "PREF_UNIQUE_ID";
public synchronized static String id(Context context) {
if (uniqueID == null) {
SharedPreferences sharedPrefs = context.getSharedPreferences(
PREF_UNIQUE_ID, Context.MODE_PRIVATE);
uniqueID = sharedPrefs.getString(PREF_UNIQUE_ID, null);
if (uniqueID == null) {
uniqueID = UUID.randomUUID().toString();
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putString(PREF_UNIQUE_ID, uniqueID);
editor.commit();
}
}
return uniqueID;
}
请帮我看看怎么做。
【问题讨论】:
-
“没有结果”是什么意思?我在我的 Android 应用程序中完美地使用了
UUID.randomUUID().toString(),为什么这对你不起作用? -
生成唯一 ID 的最佳实践:developer.android.com/training/articles/user-data-ids?hl=en 注意
UUID.randomUUID().toString()就是其中之一。 -
没有结果我的意思是它不起作用也许我错过了一些东西你能帮我看看你是如何实现它的。
-
“它不工作”是什么意思?您是否调试并检查了
uniqueID = UUID.randomUUID().toString();行以查看 uniqueID 的值?