我們使用adb shell 中的getprop 可以獲取屬性的值,但是在應(yīng)用開發(fā)中因為systemproperties的hide屬性酗宋,所以無法直接訪問到get和set函數(shù)侥蒙。
通過反射機制來獲取get和set函數(shù)摧茴,具體代碼如下:
一叹谁,設(shè)置系統(tǒng)屬性值
static public void setprop(String key, String defaultValue) {
? ? ? ? String value = defaultValue;
? ? ? ? try {
? ? ? ? ? ? Class<?> c = Class.forName("android.os.SystemProperties");
? ? ? ? ? ? Method get = c.getMethod("set", String.class, String.class );
? ? ? ? ? ? get.invoke(c, key, value );
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
二兴猩,獲取系統(tǒng)屬性值
static public String getprop(String key, String defaultValue) {
? ? ? ? String value = defaultValue;
? ? ? ? try {
? ? ? ? ? ? Class<?> c = Class.forName("android.os.SystemProperties");
? ? ? ? ? ? Method get = c.getMethod("get", String.class, String.class );
? ? ? ? ? ? value = (String)(get.invoke(c, key, "unknown" ));
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }finally {
? ? ? ? ? ? return value;
? ? ? ? }
? ? }