gpt4 book ai didi

android - 我的 Android 应用程序的唯一 ID

转载 作者:行者123 更新时间:2023-11-29 02:42:08 25 4
gpt4 key购买 nike

我想为我的 Android 应用程序创建一个唯一 ID,以便我的服务器可以识别请求来自哪个设备,并相应地向应用程序发送消息。我读到 ANDROID_ID 用作唯一标识符并不安全,因为它可能会在已获得 root 权限的设备上遭到破坏。还有一些制造商甚至不提供它。

我的 porpose 使用 UUID 安全吗?它真的是该应用程序的全局唯一 ID 吗?如果是,我计划使用 keystore 存储它,以便我可以保留它直到应用程序卸载。这是正确的方法吗?请提出建议。

最佳答案

实际上使用 UUID 是安全的,这是我创建的一个辅助函数,用于自己获取 UUID,将其保存在 Helper.java 中,因此您将调用它:

Helper.getDeviceId(context); 

也不要忘记将 String sharedPrefDbName 变量更改为您的 sharef 数据库名称,您也可以将 UUID 存储在数据库或本地文件中,以防应用程序像您说的那样被卸载。

//uuid
static String deviceId;

static String sharedPrefDbName = "MyAPPDB";

/**
* getDeviceId
* @param context
* @return String
*/
public static String getDeviceId(Context context){

//lets get device Id if not available, we create and save it
//means device Id is created once

//if the deviceId is not null, return it
if(deviceId != null){
return deviceId;
}//end

//shared preferences
SharedPreferences sharedPref = context.getSharedPreferences(sharedPrefDbName,context.MODE_PRIVATE);

//lets get the device Id
deviceId = sharedPref.getString("device_id",null);

//if the saved device Id is null, lets create it and save it

if(deviceId == null) {

//generate new device id
deviceId = generateUniqueID();

//Shared Preference editor
SharedPreferences.Editor sharedPrefEditor = sharedPref.edit();

//save the device id
sharedPrefEditor.putString("device_id",deviceId);

//commit it
sharedPrefEditor.commit();
}//end if device id was null

//return
return deviceId;
}//end get device Id


/**
* generateUniqueID - Generate Device Id
* @return
*/
public static String generateUniqueID() {

String id = UUID.randomUUID().toString();

return id;
}//end method

关于android - 我的 Android 应用程序的唯一 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43415278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com