首页 > 其他 > 详细

001. 使用SystemProperties的几种方式

时间:2021-07-10 16:03:21      阅读:14      评论:0      收藏:0      [点我收藏+]

在获取或者设置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-其他的,添加到依赖中
技术分享图片

例如:获取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依赖。

技术分享图片

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. 使用SystemProperties的几种方式

原文:https://www.cnblogs.com/chendd870172464/p/14993610.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!