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

Viewed 4853

I am using

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

but the value of return is empty.

Any idea?

Thanks

2 Answers

Are you using an emulator? Emulators return null/empty value for this system property.

Also, interesting to note some things Android/Google warns about usage of the 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.

Also, notice this warning, might be of relevance if your app targets Android Oreo or above

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);
    }

}
Related