我开发 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]
那很好。我可以看到很多值(value)观
然后当尝试使用类似的 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
没有SystemProperties
里面android.os
包裹。同时我可以使用 grepcode.com 在网上找到它,它出现在 2.3.3 到 5.L
这里有什么问题吗?这是android.jar
不好?是SystemProperties
类被隐藏了?
最佳答案
作为 Hacketo 链接到 Where is android.os.SystemProperties
android.os.SystemProperties
是隐藏的,非公共(public) API,
调用它的方法之一(尽管在某些版本中可能不可用)是使用反射。
所以代码是这样的
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) {
}
}
关于java - 使用 SystemProperties 在 Android 上获取序列号(品牌、设备等),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27034848/