简体   繁体   English

在 Android 中获取设备 ID 的最佳方法

[英]Best possible way to get device Id in Android

I just wonder what would be the best to get device id I have 2 options either I use below code that would need run time permission android.permission.READ_PHONE_STATE -我只是想知道什么是最好的获取设备 ID 我有 2 个选项或者我使用下面需要运行时权限的代码android.permission.READ_PHONE_STATE -

private String android_id = Secure.getString(getContext().getContentResolver(),
                                                        Secure.ANDROID_ID); 

Or或者

Secondly I have option of UUID that will not need any run time permission but need to be mapped with users data(like email or phone) to identify.其次,我可以选择不需要任何运行时权限但需要与用户数据(如电子邮件或电话)进行映射以识别的 UUID。

String uniqueID = UUID.randomUUID().toString();

Any suggestions which one to use and why would be appreciated.任何建议使用哪个以及为什么将不胜感激。

Thanks谢谢

ANDROID ID:安卓 ID:

On a device first boot, a random value is generated and stored.在设备首次启动时,会生成并存储一个随机值。 This value is available via Settings.Secure.ANDROID_ID.此值可通过 Settings.Secure.ANDROID_ID 获得。 It's a 64-bit number that should remain constant for the lifetime of a device.它是一个 64 位数字,应该在设备的生命周期内保持不变。 ANDROID_ID seems a good choice for a unique device identifier because it's available for smartphones and tablets. ANDROID_ID 似乎是唯一设备标识符的不错选择,因为它可用于智能手机和平板电脑。

Issues: However, the value may change if a factory reset is performed on the device.问题:但是,如果在设备上执行恢复出厂设置,该值可能会更改。 There is also a known bug with a popular handset from a manufacturer where every instance has the same ANDROID_ID.制造商的流行手机也存在一个已知错误,其中每个实例都具有相同的 ANDROID_ID。 Clearly, the solution is not 100% reliable.显然,该解决方案并非 100% 可靠。

UUID: UUID:

As the requirement for most applications is to identify a particular installation and not a physical device, a good solution to get a unique id for a user if to use the UUID class.由于大多数应用程序的要求是识别特定安装而不是物理设备,因此如果使用 UUID 类,则是为用户获取唯一 ID 的好方法。

Issues: UUID.randomUUID() method generates a unique identifier for a specific installation.问题: UUID.randomUUID() 方法为特定安装生成唯一标识符。 You have just to store that value and your user will be identified at the next launch of your application.您只需存储该值,您的用户将在您的应用程序下次启动时被识别。 You can also try to associate this solution with the Android Backup service to keep the information available for the user even if he installs your application on another device.您还可以尝试将此解决方案与 Android 备份服务相关联,以确保用户可以使用这些信息,即使他将您的应用程序安装在另一台设备上。

CONCLUSION:结论:

Identify a particular device on Android is not an easy thing.在 Android 上识别特定设备并非易事。 There are many good reasons to avoid that.有很多很好的理由可以避免这种情况。 The best solution is probably to identify a particular installation by using the UUID solution.最好的解决方案可能是使用 UUID 解决方案来识别特定的安装。 However, if you want absolutely identify a particular device physically, you can try to use the ANDROID_ID solution.但是,如果您想从物理上绝对识别特定设备,则可以尝试使用 ANDROID_ID 解决方案。 Not 100% reliable but better than another solution.不是 100% 可靠,但比其他解决方案更好。

you can use UUID class like this :您可以像这样使用 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;
}

then take a look at here : https://www.pocketmagic.net/android-unique-device-id/ that Pseudo-Unique ID and Combined Device ID is not bad, maybe ;然后看看这里: https : //www.pocketmagic.net/android-unique-device-id/那个伪唯一ID组合设备ID还不错,也许吧;

ADROID_ID is the best way to get android device id it will be same until factory reset. ADROID_ID 是获取 android 设备 ID 的最佳方式,它会在恢复出厂设置之前保持不变。

    public static String getDeviceId(Context context) {

    String id = Settings.Secure.getString(context.getContentResolver(),
            Settings.Secure.ANDROID_ID);
    return id;
}

To get Device ID you can use要获取设备 ID,您可以使用

fun getAndroidId(context: Context): String {
    val androidId =
        Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID)
    return androidId
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM