public class SystemPropertiesProxy { public static String get(String key) { String result=""; try { Class<?> c = Class.forName("android.os.SystemProperties"); Method get = c.getMethod("get", String.class); result=(String)get.invoke(c, key); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } return result; } public static void set(String key, String val) { try { Class<?> c = Class.forName("android.os.SystemProperties"); Method set = c.getMethod("set", String.class,String.class); set.invoke(c, key,val); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } }
调用:
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); SystemPropertiesProxy.set("your key", "your value"); String content = SystemPropertiesProxy.get("your key"); Log.e("eee", "your value is: " + content); } }