https://blog.csdn.net/ouzhuangzhuang/article/details/82258148
需求
需要在不同應用中定義一個標志位,這里介紹下系統級別的應用和非系統級別應用如何添加。當然這不一定是最好的辦法,因為不能夠添加intent.putExtra()屬性。
系統級別應用
在需要定義的地方使用 SystemProperties.set(“dev.xxx.xxx”, “false”);
在獲取的部分使用 SystemProperties.getBoolean(“ro.mmitest”, false))
最后記得要導包 import android.os.SystemProperties
非系統級別應用
在需要定義的地方使用 Settings.Global.putInt(context.getContentResolver(),“xxx.xxx”,1);
在獲取的部分使用
boolean mTag = Settings.Global.getInt(getActivity().getContentResolver(),“xxx.xxx”, 0) == 1;
依舊別忘記導包 import android.provider.Settings;
SystemProperties不能直接使用需要通過反射機制:
https://blog.csdn.net/wenzhi20102321/article/details/80503568/
public final class ReflectUtil { public static String getProperty(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, defaultValue)); } catch (Exception e) { e.printStackTrace(); }finally { return value; } } public static void setProperty(String key, String value) { try { Class<?> c = Class.forName("android.os.SystemProperties"); Method set = c.getMethod("set", String.class, String.class); set.invoke(c, key, value); } catch (Exception e) { e.printStackTrace(); } } }
//獲取屬性,判斷設備是否可以實現wifi adb功能 String property = ReflectUtil.getProperty("persist.adb.tcp.port", "0"); Log.i(TAG, "property : " + property); //設置設備可以使用WiFi adb功能 ReflectUtil.setProperty("persist.adb.tcp.port", "5555"); //關閉設備WiFi adb功能 ReflectUtil.setProperty("persist.adb.tcp.port", "0");