【问题标题】:Unique Id for my android app我的 android 应用程序的唯一 ID
【发布时间】:2017-04-14 16:13:36
【问题描述】:

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

UUID 可以安全地用于我的 porpose 吗?它真的是应用程序的全球唯一 id 吗?如果是,我打算使用密钥库存储它,以便在应用程序卸载之前保留它。是不是正确的做法。请提出建议。

【问题讨论】:

  • developer.android.com/training/articles/user-data-ids.html "我打算使用密钥库存储它,以便在应用程序卸载之前保留它" -- 我希望应用程序的密钥库条目在应用程序的内部存储被删除时被删除已清除,所以我不确定这对您是否有好处。
  • developer.android.com/training/articles/user-data-ids.html唯一ID也存在隐私问题,请完整阅读本文档
  • 只要安装了我的应用程序,我就想保留这个 ID。在卸载它的确定重置它。感谢关于 ids 的精彩文章。他们谈论为我的目的使用实例 ID 或 UUID。可以使用其中任何一个吗?其他任何已知的优点/缺点?请提出建议。

标签: android uuid uniqueidentifier


【解决方案1】:

使用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

【讨论】:

  • 感谢您的回复。我正在阅读我们可以使用的不同类型的 ID。出于我的目的,实例 ID 或 UUID 很有用。但如果是这样的话,哪个更好?
  • 经过一番研究,我最终选择了 UUID。这是来自 android dev (developer.android.com/training/articles/…) 的引用:“#3:尽可能使用实例 ID 或私有存储的 GUID 用于所有其他用途-除了支付欺诈预防和电话之外的情况。对于绝大多数非广告用例,实例 ID 或 GUID 就足够了。”,所以在我的情况下,UUID 是完美的,因为我需要跟踪用户的存在和设备更改,我将本地 UUID 同步到我的远程服务器,因此应用程序每隔 x 秒确认一次是否..
  • ..它们匹配,如果不匹配,则需要新的 sms 身份验证来验证新设备上的用户,并重新生成新的 UUID 并重新同步到我的服务器。这有助于防止多个设备使用相同的帐户,我的灵感来自 whatsapp ..
  • 如果用户清除了他们的 SharedPref,那么device_id 就会丢失
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-04
  • 2015-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多