zoukankan      html  css  js  c++  java
  • 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");
     
  • 相关阅读:
    Ubuntu 上更新 Flash 插件
    Ubuntu 17.10 安装 “爱壁纸” 时,缺失了 python-support 依赖
    Windows 7 64 位操作系统安装 Ubuntu 17.10
    Linux CentOS 6.9(图形界面)安装中文输入法
    Linux 编译 apr-util 时报错
    Linux 添加普通用户到 sudoers 文件
    PHP 结合实例认识 Socket
    PHP 快速建立一个对象
    使用Git GUI,上传项目到github,并实现预览功能
    用JS判断号码
  • 原文地址:https://www.cnblogs.com/mwl523/p/14133471.html
Copyright © 2011-2022 走看看