• 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");
     
  • 相关阅读:
    hdu 1711Number Sequence
    hdu 4911Inversion
    DataView数据变化的各种状态
    c#中的dataview数据视图的sort属性进行排序,用rowfilter属性进行筛选,完成学生档案信息的显示。
    DataView.RowFilter筛选DataTable中的数据
    C# 递归产生树
    treeview递归绑定的两种方法
    C#递归加载树
    c# DropDownList 下拉框实现树形导航
    C# ComboBox 下拉显示层次(树)
  • 原文地址:https://www.cnblogs.com/mwl523/p/14133471.html
Copyright © 2020-2023  润新知