【问题标题】:Application Generate UUID?应用程序生成 UUID?
【发布时间】:2012-03-31 15:08:21
【问题描述】:

我想我需要更多地了解 UUID 的实际工作原理。我正在开发一个应用程序,我希望该应用程序在用户第一次下载并运行该应用程序时生成一个 UUID。是否可以在用户每次下载应用时生成一个新的 uuid?

http://developer.android.com/reference/java/util/UUID.html 也许如果除了android开发人员之外还有另一个网站我可以理解或查看使用uuid的sombody示例,sombody可以发布吗?谢谢。

【问题讨论】:

  • 这个post应该回答你的问题。
  • 好的,所以这篇文章非常好,我正在浏览代码,但我无法理解文件实际写入 UUID 的位置。它是否被写入“安装”文件夹中

标签: java android hash hashmap uuid


【解决方案1】:

这里是生成 UUID 的代码:

String android_id = Secure.getString(getApplicationContext()
            .getContentResolver(), Secure.ANDROID_ID);
    Log.i("System out", "android_id : " + android_id);

    final TelephonyManager tm = (TelephonyManager) getBaseContext()
            .getSystemService(Context.TELEPHONY_SERVICE);

    final String tmDevice, tmSerial, androidId;
    tmDevice = "" + tm.getDeviceId();
    Log.i("System out", "tmDevice : " + tmDevice);
    tmSerial = "" + tm.getSimSerialNumber();
    Log.i("System out", "tmSerial : " + tmSerial);
    androidId = ""
            + android.provider.Settings.Secure.getString(
                    getContentResolver(),
                    android.provider.Settings.Secure.ANDROID_ID);

    UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice
            .hashCode() << 32)
            | tmSerial.hashCode());
    String UUID = deviceUuid.toString();
    Log.i("System out", "UUID : " + UUID);

【讨论】:

  • 我会在 tm 上添加对 null 的检查,因为如果特定设备上不存在请求服务,getSystemService 可能会返回 null。
【解决方案2】:
UUID uuid = UUID.randomUUID();

这应该会为您生成一个随机 UUID,供您随意使用。

【讨论】:

  • 我希望我在 3 年前就有这个答案。我知道了,谢谢。
  • 哈哈我真的来到了这个帖子,因为我一直在寻找答案,然后偶然在 Android 开发者文档中找到了.randomUUID(),并认为 id 在这里发布它很可能出现在更新版本的 SDK。
  • 每次调用它都会生成一个随机的 UUID 最好只调用一次并存储它返回的值。
猜你喜欢
  • 2015-09-30
  • 1970-01-01
  • 2016-10-23
  • 2017-06-25
  • 2013-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多