【问题标题】:using SystemProperties to get Serial (Brand, Device.. etc) on Android [duplicate]使用 SystemProperties 在 Android 上获取序列号(品牌、设备..等)[重复]
【发布时间】:2015-01-18 00:48:53
【问题描述】:

我开发 Android 应用来显示一些基本信息。

查看来源,我发现android.os.Build 具有大部分需要的值

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

要查看本地连接到 PC 设备上的这些值,我可以adb shell getprop

[ro.product.name]:[A828t]

这很好。而且我可以看到很多价值观

然后当尝试使用类似的getString 从 Java 中读取时,就像在 android.os.Build 中一样

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

我发现 Android 4.4.2(以及 4.3、2.3.3 中)android.jar 的源代码在 android.os 包内没有 SystemProperties。同时我可以使用grepcode.com在线找到它,它从 2.3.3 到 5.L

这里有什么问题?这是android.jar 不好吗? SystemProperties 类被隐藏了吗?

【问题讨论】:

标签: java android system-properties


【解决方案1】:

作为 Hacketo 链接到 Where is android.os.SystemProperties

android.os.SystemProperties 是隐藏的,非公共 API,

可以在原始版本http://grepcode.com/file_/repository.grepcode.com/java/ext/com.google.android/android/2.3_r1/android/os/SystemProperties.java/?v=source中看到

调用它的一种方法(尽管在某些版本中可能不可用)是使用反射。
所以代码是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) {
         }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-09
    • 1970-01-01
    • 2015-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多