001. 使用SystemProperties的几种方式

在获取或者设置android的一些系统属性时,需要用到android.os.SystemProperties.get或者set方法,但是SystemProperties这个api是隐藏的,普通应用无法正常使用。

/**
 * Gives access to the system properties store.  The system properties
 * store contains a list of string key-value pairs.
 *
 * {@hide}
 */
public class SystemProperties

可以通过以下几种方式进行操作

1. 使用反射获取

import java.lang.reflect.Method;

public class PropUtils {

    /**
     * 设置属性值
     *
     * @param key   长度不能超过31,key.length <= 30
     * @param value 长度不能超过91,value.length<=90
     */
    public static void set(String key, String value) {
        // android.os.SystemProperties
        // public static void set(String key, String val)
        try {
            Class<?> cls = Class.forName("android.os.SystemProperties");
            Method method = cls.getMethod("set", String.class, String.class);
            method.invoke(null, key, value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取属性值
     *
     * @param key 长度不能超过31,key.length <= 30
     * @param defValue
     * @return
     */
    public static String get(String key, String defValue) {
        // android.os.SystemProperties
        // public static String get(String key, String def)
        try {
            Class<?> cls = Class.forName("android.os.SystemProperties");
            Method method = cls.getMethod("get", String.class, String.class);
            return (String) method.invoke(null, key, defValue);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return defValue;
    }

}

例如:获取ro.product.model属性值

public class MainActivity extends AppCompatActivity {
    private final String TAG = getClass().getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String model = PropUtils.get("ro.product.model", "");
        Log.d(TAG, "onCreate: " + model);
    }
}

2. 添加layoutlib.jar进行使用

jar路径:D:\Android\sdk\platforms\android-25\data,也可以使用android-其他的,添加到依赖中
001. 使用SystemProperties的几种方式

例如:获取ro.product.model属性值

public class MainActivity extends AppCompatActivity {
    private final String TAG = getClass().getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String model = SystemProperties.get("ro.product.model", "");
        Log.d(TAG, "onCreate: " + model);
    }
}

3. gradle进行配置操作

在模块的build.gradle中进行配置,这种方式和2类似,也是添加了jar依赖。

001. 使用SystemProperties的几种方式

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

    implementation files(SDK_DIR() + "/platforms/android-25/data/layoutlib.jar")
}

def SDK_DIR() {
    String ret = System.getenv("ANDROID_SDK_HOME")
    if (null == ret) {
        Properties props = new Properties()
        props.load(new FileInputStream(project.rootProject.file("local.properties")))
        ret = props.get('sdk.dir')
    }
    return ret
}

上一篇:001、pytest环境、用例设计原则、Pycharm设置pytest运行器


下一篇:python算法100题001