1,System.getProperty(),此方法可以获取系统的一些系统属性,而这些属性并不是我们在终端中执行shell脚本(getprop)时得到的属性,列出小部分属性:
root@android:/ # getprop [alsa.mixer.capture.headset]: [Capture][alsa.mixer.capture.master]: [Capture][alsa.mixer.playback.headset]: [Headphone][alsa.mixer.playback.master]: [Playback][alsa.mixer.playback.speaker]: [Playback][athr.gps.conf]: [/data/app/ing/Orion.ini][athr.gps.hookspath]: [/system/etc][back_camera_name]: [ov][back_camera_orient]: [0][dalvik.vm.dexopt-flags]: [m=y][dalvik.vm.heapgrowthlimit]: [48m][dalvik.vm.heapsize]: [256m][dalvik.vm.heapstartsize]: [5m][dalvik.vm.stack-trace-file]: [/data/anr/traces.txt][debug.egl.hw]: [1][debug.sf.hw]: [1][debug.sf.showfps]: [0]所以想要获取getprop中的属性,此方法是行不通的。
/** * Returns the value of a particular system property or {@code null} if no * such property exists. */ public static String getProperty(String propertyName) { return getProperty(propertyName, null); } /** * Returns the value of a particular system property. The {@code * defaultValue} will be returned if no such property has been found. */ public static String getProperty(String prop, String defaultValue) { if (prop.isEmpty()) { throw new IllegalArgumentException(); } return getProperties().getProperty(prop, defaultValue); }此方法最终调用getProperties()方法,从getPropertyes()可以得知是使用一个单列,返回一个Properties,在其初始方法initSystemProperties()中就可以得知System.getProperty()可以获取那些属性。
/** * Returns the system properties. Note that this is not a copy, so that * changes made to the returned Properties object will be reflected in * subsequent calls to getProperty and getProperties. * * @return the system properties. */ public static Properties getProperties() { if (systemProperties == null) { initSystemProperties(); } return systemProperties; }2,SystemProperties.get(),此方法可以获取shell命令getprop中的属性,从源码来看,两者处理方式是完全不同的,framework通过SystemProperties接口操作系统属性,SystemProperties通过JNI调用访问系统属性。
/** * Get the value for the given key. * @return if the key isn't found, return def if it isn't null, or an empty string otherwise * @throws IllegalArgumentException if the key exceeds 32 characters */ public static String get(String key, String def) { if (key.length() > PROP_NAME_MAX) { throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX); } return native_get(key, def); }
/** * Set the value for the given key. * @throws IllegalArgumentException if the key exceeds 32 characters * @throws IllegalArgumentException if the value exceeds 92 characters */ public static void set(String key, String val) { if (key.length() > PROP_NAME_MAX) { throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX); } if (val != null && val.length() > PROP_VALUE_MAX) { throw new IllegalArgumentException("val.length > " + PROP_VALUE_MAX); } native_set(key, val); }但需要注意的是,此方法我只在系统源码中可以使用,在eclipse中引用不到SystemProperties,所以我们还是美发轻易获取到getprop中的系统属性;但对于源码开发来说,此方法真的比较方便,还提供了get(key, def),getInt(),getLong(),getboolean()方法,直接可以获取你想要的返回类型。
3,在java代码中执行shell命令,Runtime.getRuntime().exec("getprop propertyName"),这是我目前在eclipse中处理获取系统属性的方法,各位大如有其他好的方法,本人求科普,求分享。。
Process du = Runtime.getRuntime().exec("getprop gps.exist"); BufferedReader in = new BufferedReader(new InputStreamReader(du.getInputStream())); String gpsvalue = in.readLine();
参考: