Java 在Android中获取设备Id的最佳方法
我只是想知道什么是获取设备id的最佳方式我有两个选项,或者我使用下面的代码,需要运行时权限Java 在Android中获取设备Id的最佳方法,java,android,Java,Android,我只是想知道什么是获取设备id的最佳方式我有两个选项,或者我使用下面的代码,需要运行时权限android.permission.READ\u PHONE\u STATE- private String android_id = Secure.getString(getContext().getContentResolver(), Secure.ANDROID_ID); 或 其次,我
android.permission.READ\u PHONE\u STATE
-
private String android_id = Secure.getString(getContext().getContentResolver(),
Secure.ANDROID_ID);
或
其次,我可以选择UUID,它不需要任何运行时权限,但需要与用户数据(如电子邮件或电话)进行映射以识别
String uniqueID = UUID.randomUUID().toString();
如有任何建议,请使用哪一种以及使用原因,我们将不胜感激
谢谢您可以像这样使用UUID类:
private static String uniqueID = null;
private static final String PREF_UNIQUE_ID = "PREF_UNIQUE_ID";
public synchronized static String id(Context context) {
if (uniqueID == null) {
SharedPreferences sharedPrefs = context.getSharedPreferences(
PREF_UNIQUE_ID, Context.MODE_PRIVATE);
uniqueID = sharedPrefs.getString(PREF_UNIQUE_ID, null);
if (uniqueID == null) {
uniqueID = UUID.randomUUID().toString();
Editor editor = sharedPrefs.edit();
editor.putString(PREF_UNIQUE_ID, uniqueID);
editor.commit();
}
} return uniqueID;
}
然后看看这里:伪唯一ID和组合设备ID可能不错 ANDROID ID: 在设备首次启动时,会生成并存储一个随机值。此值可通过Settings.Secure.ANDROID_ID获得。它是一个64位数字,在设备的生命周期内应保持不变。ANDROID_ID似乎是唯一设备标识符的好选择,因为它适用于智能手机和平板电脑 问题: 但是,如果在设备上执行出厂重置,则该值可能会改变。还有一个已知的错误,来自一家制造商的流行手机,每个实例都有相同的ANDROID_ID。显然,解决方案不是100%可靠的 UUID: 由于大多数应用程序的要求是识别特定的安装,而不是物理设备,因此如果要使用UUID类,为用户获取唯一id是一个很好的解决方案 问题: 方法生成特定安装的唯一标识符。您只需存储该值,您的用户将在下一次启动应用程序时被识别。您还可以尝试将此解决方案与Android备份服务相关联,以便即使用户在其他设备上安装了您的应用程序,也可以为用户保留可用的信息 结论:
在安卓系统上识别特定设备并非易事。有很多很好的理由可以避免这种情况。最好的解决方案可能是使用UUID解决方案来识别特定的安装。但是,如果您想绝对地在物理上识别特定的设备,您可以尝试使用ANDROID_ID解决方案。不是100%可靠,但比其他解决方案更好。要获取设备ID,您可以使用
fun getAndroidId(context: Context): String {
val androidId =
Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID)
return androidId
}
ADROID_ID是获取android设备ID的最佳方式,在出厂重置之前,它将保持不变
public static String getDeviceId(Context context) {
String id = Settings.Secure.getString(context.getContentResolver(),
Settings.Secure.ANDROID_ID);
return id;
}
这回答了你的问题吗?谢谢你的回答-我只是想知道哪种方式最适合使用,为什么。谢谢你给我时间详细解释。