我的安卓应用需要一个唯一标识符

3
我想为我的安卓应用创建一个唯一的ID,以便我的服务器能够识别请求来自哪个设备,并相应地向应用发送消息。我了解到ANDROID_ID不安全,因为在Rooted设备上可能会被破解。而且有些制造商甚至没有提供它。
UUID对我的目的来说安全吗?它真的是应用程序的全局唯一标识符吗?如果是的话,我计划使用密钥库存储它,以便可以在应用程序卸载之前保留它。这是否是正确的方法?请建议。

我计划使用密钥库来存储它,以便在应用程序卸载之前可以保留它。但是,我希望当应用程序的内部存储被清除时,应用程序的密钥库条目也会被删除,因此我不确定这对您是否有益。 - CommonsWare
https://developer.android.com/training/articles/user-data-ids.html 唯一标识符也涉及隐私问题,请完整阅读本文档。 - Mohammed Atif
我想在我的应用程序安装期间保留此ID。在卸载时重置它是可以的。感谢您关于ID的精彩文章。他们谈论使用Instance ID或UUID来实现我的目的。使用它们中的任何一个都可以吗?另一个有什么已知的优缺点吗?请给予建议。 - MobileAppDeveloper
1个回答

2

实际上使用UUID是安全的,这是我创建的一个帮助函数来获取UUID,将其保留在Helper.java中,因此您可以调用它:

Helper.getDeviceId(context); 

不要忘记将String sharedPrefDbName变量更改为您的共享数据库名称,您还可以将UUID存储在DB或本地文件中,以防应用程序被卸载,就像您说的那样。
//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

谢谢回复。我正在阅读关于我们可以使用的不同类型的ID的内容。对于我的目的,实例ID或UUID是有用的。但如果情况是这样,哪一个更好呢? - MobileAppDeveloper
经过一些研究,我最终选择了UUID。这是来自Android开发者的引用(https://developer.android.com/training/articles/user-data-ids.html#version_specific_details_identifiers_in_m):“#3:除了支付欺诈预防和电话之外,在所有其他用例中尽可能使用实例ID或私有存储的GUID。对于绝大多数非广告用例,实例ID或GUID应该足够。”因此,在我的情况下,UUID非常完美,因为我需要跟踪用户存在和设备更改,我将本地UUID与远程服务器同步,因此每隔x秒钟应用程序会确认是否... - Big Zak
如果它们匹配,那么一切正常;如果不匹配,则需要进行新的短信认证以在新设备上验证用户,并重新生成和同步新的UUID到我的服务器。这有助于防止多个设备使用同一个帐户,我从WhatsApp中获得了灵感。 - Big Zak
如果用户清除了他们的SharedPref,则device_id将丢失。 - IgorGanapolsky

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接