如何在应用程序中获取 Android 手机的 UUID?
我正在寻求帮助来获取 Android 手机的 UUID。我在网上搜索并找到了一种潜在的解决方案,但它在模拟器中不起作用。
这是代码:
Class<?> c;
try {
c = Class.forName("android.os.SystemProperties");
Method get = c.getMethod("get", String.class);
serial = (String) get.invoke(c, "ro.serialno");
Log.d("ANDROID UUID",serial);
} catch (Exception e) {
e.printStackTrace();
}
有人知道为什么它不起作用,或者有更好的解决方案吗?
I'm looking for help to get the UUID of my Android phone. I have searched the net and found one potential solution but it is not working in the emulator.
Here is the code:
Class<?> c;
try {
c = Class.forName("android.os.SystemProperties");
Method get = c.getMethod("get", String.class);
serial = (String) get.invoke(c, "ro.serialno");
Log.d("ANDROID UUID",serial);
} catch (Exception e) {
e.printStackTrace();
}
Does anybody know why it isn't working, or have a better solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
正如 Dave Webb 提到的,Android 开发者博客有一篇文章 涵盖了这一点。他们首选的解决方案是跟踪应用程序安装而不是设备,这对于大多数用例都适用。这篇博客文章将向您展示实现该功能所需的代码,我建议您查看一下。
但是,如果您需要设备标识符而不是应用程序安装标识符,该博客文章继续讨论解决方案。我与 Google 的某人进行了交谈,以便在您需要时对一些项目进行一些额外的说明。以下是我在上述博客文章中未提及的有关设备标识符的发现:
根据 Google 的建议,我实现了一个类,该类将为每个设备生成唯一的 UUID,在适当的情况下使用 ANDROID_ID 作为种子,必要时依靠 TelephonyManager.getDeviceId() ,如果失败,则诉诸随机生成的唯一 UUID该信息在应用程序重新启动后仍然存在(但不是应用程序重新安装)。
请注意,对于必须回退设备 ID 的设备,唯一 ID将在恢复出厂设置后仍然保留。这是需要注意的事情。如果您需要确保恢复出厂设置会重置您的唯一 ID,您可能需要考虑直接回退到随机 UUID,而不是设备 ID。
同样,此代码用于设备 ID,而不是应用程序安装 ID。在大多数情况下,您可能需要的是应用程序安装 ID。但如果您确实需要设备 ID,那么以下代码可能适合您。
As Dave Webb mentions, the Android Developer Blog has an article that covers this. Their preferred solution is to track app installs rather than devices, and that will work well for most use cases. The blog post will show you the necessary code to make that work, and I recommend you check it out.
However, the blog post goes on to discuss solutions if you need a device identifier rather than an app installation identifier. I spoke with someone at Google to get some additional clarification on a few items in the event that you need to do so. Here's what I discovered about device identifiers that's NOT mentioned in the aforementioned blog post:
Based on Google's recommendations, I implemented a class that will generate a unique UUID for each device, using ANDROID_ID as the seed where appropriate, falling back on TelephonyManager.getDeviceId() as necessary, and if that fails, resorting to a randomly generated unique UUID that is persisted across app restarts (but not app re-installations).
Note that for devices that have to fallback on the device ID, the unique ID WILL persist across factory resets. This is something to be aware of. If you need to ensure that a factory reset will reset your unique ID, you may want to consider falling back directly to the random UUID instead of the device ID.
Again, this code is for a device ID, not an app installation ID. For most situations, an app installation ID is probably what you're looking for. But if you do need a device ID, then the following code will probably work for you.
这对我有用:
编辑:
您还需要在清单中设置
android.permission.READ_PHONE_STATE
。从 Android M 开始,您需要在运行时请求此权限。请参阅此答案:https://stackoverflow.com/a/38782876/1339179
This works for me:
EDIT :
You also need
android.permission.READ_PHONE_STATE
set in your Manifest. Since Android M, you need to ask this permission at runtime.See this anwser : https://stackoverflow.com/a/38782876/1339179
使用 ANDROID_ID 代替从 TelephonyManager 获取 IMEI。
这适用于每个 Android 设备,无论是否有电话功能。
Instead of getting IMEI from TelephonyManager use ANDROID_ID.
This works for each android device irrespective of having telephony.
请参阅 Android 开发者博客文章,了解如何使用 UUID 类获取 uuid
See Android Developer blog article for using UUID class to get uuid
从 API 26 开始,getDeviceId() 已被弃用。如果您需要获取设备的 IMEI,请使用以下命令:
As of API 26, getDeviceId() is deprecated. If you need to get the IMEI of the device, use the following:
添加
方法
Add
Method
我认为您可以在技术上使用 ANDROID_ID 生成一个唯一的 ID,例如:
I think you can technically generate a unique one using the ANDROID_ID, something like: