Search code examples
javaandroidsystem-properties

using SystemProperties to get Serial (Brand, Device.. etc) on Android


I develop Android app to show some basic info.

Looking at sources, I discovered that android.os.Build has most of needed values

/** The name of the overall product. */
public static final String PRODUCT = getString("ro.product.name");

to take a look at those values on locally connected to PC device I can adb shell getprop

[ro.product.name]: [A828t]

That is good. And I can see a lot a values

Then when trying to read from Java using similar getString like in android.os.Build

private static String getString(String property) {
    return SystemProperties.get(property, UNKNOWN);
}

I found that sources for Android 4.4.2 (and in 4.3, 2.3.3 as well) android.jar does not have SystemProperties inside android.os package. In the same time i can find it online using grepcode.com, where it is present from 2.3.3 to 5.L

What the problem here? Is this android.jar not good? Was the SystemProperties class made hidden?


Solution

  • as Hacketo linked to Where is android.os.SystemProperties

    android.os.SystemProperties is hidden, non public API,

    that can be seen at raw version http://grepcode.com/file_/repository.grepcode.com/java/ext/com.google.android/android/2.3_r1/android/os/SystemProperties.java/?v=source

    One of the ways to call it (though may be not available in some versions) is using reflection.
    So the code is ike

        String serial = null;       
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
            serial = Build.SERIAL;
        } else {
            try {
                 Class<?> c = Class.forName("android.os.SystemProperties");
                 Method get = c.getMethod("get", String.class);
                 serial = (String) get.invoke(c, "ro.serialno");
             } catch (Exception ignored) {
             }
        }