Android 利用Settings.Global属性跨应用定义标志位

 

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");
 

 

posted @ 2020-12-14 15:25  mao的博客  阅读(6028)  评论(0编辑  收藏  举报