为什么返回 Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID) 一个空字符串;

Why is returning Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID) an empty string;

我正在使用

Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);

但是 return 的值为空。

有什么想法吗?

谢谢

您使用的是模拟器吗?此系统的模拟器 return null/empty 值 属性。

此外,有趣的是要注意一些事情 Android/Google 警告使用 ANDROID_ID here

ANDROID_ID seems a good choice for a unique device identifier. There are downsides: First, it is not 100% reliable on releases of Android prior to 2.2 (“Froyo”). Also, there has been at least one widely-observed bug in a popular handset from a major manufacturer, where every instance has the same ANDROID_ID.

此外,请注意此 warning,如果您的应用面向 Android Oreo 或更高版本

,则可能具有相关性

Note: For apps that were installed prior to updating the device to a version of Android 8.0 (API level 26) or higher, the value of ANDROID_ID changes if the app is uninstalled and then reinstalled after the OTA. To preserve values across uninstalls after an OTA to Android 8.0 or higher, developers can use Key/Value Backup.

 public static String getDeviceID(Context Ctx) {

    String android_id = Secure.getString(Ctx.getContentResolver(),
            Secure.ANDROID_ID);
    return android_id;
}

public static String getImeiNumber(Context Ctx) {
    final TelephonyManager telephonyManager = (TelephonyManager) Ctx.getSystemService(Context.TELEPHONY_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        //getDeviceId() is Deprecated so for android O we can use getImei() method
        return telephonyManager.getImei();
    } else {
        return getDeviceID(Ctx);
    }

}