【问题标题】:How can i generate a user id in android如何在android中生成用户ID
【发布时间】: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 的值?

标签: java android


【解决方案1】:

您可以使用唯一的 android 设备 ID:

private String android_id = Secure.getString(getContext().getContentResolver(), Secure.ANDROID_ID); 

【讨论】:

  • 它对我有用,即使用户删除应用程序并重新安装它也是不变的。
【解决方案2】:

您可以尝试创建自动增量方法。这样你就不会得到双打。

C# Class Auto increment ID

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-14
    • 2019-02-05
    • 2021-03-22
    相关资源
    最近更新 更多