如何在Android中生成用户ID

时间:2019-02-15 18:57:03

标签: java android

我正在开发一个应用程序,在该应用程序中我需要为每个用户指定一个用户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;
}

请帮助我完成操作。

2 个答案:

答案 0 :(得分:0)

您可以尝试创建自动递增方法。这样一来,您就不会双打。

C# Class Auto increment ID

答案 1 :(得分:0)

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

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