Android通过SystemProperties获取build.prop中配置的信息

Posted mufeng_慕枫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android通过SystemProperties获取build.prop中配置的信息相关的知识,希望对你有一定的参考价值。

前言

        我们知道system.prop文件中存储着系统运行的很多配置信息(具体特定平台或产品的修改信息),system.prop文件中的内容最终会被编译到build.prop文件中,当程序运行时需要某种系统状态时,会到build.prop中进行读取。android中提供了一个android.os.SystemProperties类来负责读取其中的内容,并且提供了几个Static的方法,可以让应用很方便地使用。

系统源码展示

/*
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.os;

import java.util.ArrayList;


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

    public static final int PROP_NAME_MAX = 31;
    public static final int PROP_VALUE_MAX = 91;

    private static final ArrayList<Runnable> sChangeCallbacks = new ArrayList<Runnable>();

    private static native String native_get(String key);
    private static native String native_get(String key, String def);
    private static native int native_get_int(String key, int def);
    private static native long native_get_long(String key, long def);
    private static native boolean native_get_boolean(String key, boolean def);
    private static native void native_set(String key, String def);
    private static native void native_add_change_callback();

    /**
     * Get the value for the given key.
     * @return an empty string if the key isn't found
     * @throws IllegalArgumentException if the key exceeds 32 characters
     */
    public static String get(String key) 
        if (key.length() > PROP_NAME_MAX) 
            throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
        
        return native_get(key);
    

    /**
     * Get the value for the given key.
     * @return if the key isn't found, return def if it isn't null, or an empty string otherwise
     * @throws IllegalArgumentException if the key exceeds 32 characters
     */
    public static String get(String key, String def) 
        if (key.length() > PROP_NAME_MAX) 
            throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
        
        return native_get(key, def);
    

    /**
     * Get the value for the given key, and return as an integer.
     * @param key the key to lookup
     * @param def a default value to return
     * @return the key parsed as an integer, or def if the key isn't found or
     *         cannot be parsed
     * @throws IllegalArgumentException if the key exceeds 32 characters
     */
    public static int getInt(String key, int def) 
        if (key.length() > PROP_NAME_MAX) 
            throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
        
        return native_get_int(key, def);
    

    /**
     * Get the value for the given key, and return as a long.
     * @param key the key to lookup
     * @param def a default value to return
     * @return the key parsed as a long, or def if the key isn't found or
     *         cannot be parsed
     * @throws IllegalArgumentException if the key exceeds 32 characters
     */
    public static long getLong(String key, long def) 
        if (key.length() > PROP_NAME_MAX) 
            throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
        
        return native_get_long(key, def);
    

    /**
     * Get the value for the given key, returned as a boolean.
     * Values 'n', 'no', '0', 'false' or 'off' are considered false.
     * Values 'y', 'yes', '1', 'true' or 'on' are considered true.
     * (case sensitive).
     * If the key does not exist, or has any other value, then the default
     * result is returned.
     * @param key the key to lookup
     * @param def a default value to return
     * @return the key parsed as a boolean, or def if the key isn't found or is
     *         not able to be parsed as a boolean.
     * @throws IllegalArgumentException if the key exceeds 32 characters
     */
    public static boolean getBoolean(String key, boolean def) 
        if (key.length() > PROP_NAME_MAX) 
            throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
        
        return native_get_boolean(key, def);
    

    /**
     * Set the value for the given key.
     * @throws IllegalArgumentException if the key exceeds 32 characters
     * @throws IllegalArgumentException if the value exceeds 92 characters
     */
    public static void set(String key, String val) 
        if (key.length() > PROP_NAME_MAX) 
            throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
        
        if (val != null && val.length() > PROP_VALUE_MAX) 
            throw new IllegalArgumentException("val.length > " +
                PROP_VALUE_MAX);
        
        native_set(key, val);
    

    public static void addChangeCallback(Runnable callback) 
        synchronized (sChangeCallbacks) 
            if (sChangeCallbacks.size() == 0) 
                native_add_change_callback();
            
            sChangeCallbacks.add(callback);
        
    

    static void callChangeCallbacks() 
        synchronized (sChangeCallbacks) 
            //Log.i("foo", "Calling " + sChangeCallbacks.size() + " change callbacks!");
            if (sChangeCallbacks.size() == 0) 
                return;
            
            ArrayList<Runnable> callbacks = new ArrayList<Runnable>(sChangeCallbacks);
            for (int i=0; i<callbacks.size(); i++) 
                callbacks.get(i).run();
            
        
    

工具类源码展示

      因为SystemProperties.java类已被系统隐藏,因此我们通过Java反射机制获取该类内容,通过get和set方法来读取、设置build.prop里面的内容。

package com.mufeng.testproject.tools;

import android.content.Context;
import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;

import dalvik.system.DexFile;

/**
 * Created by zhangqing on 2017/3/1.
 */
public class SystemPropertiesProxy 
    public static final String TAG = "SystemPropertiesProxy";

    /**
     * 根据给定的Key返回String类型的值
     *
     * @param context 上下文
     * @param key     获取指定信息所需的key
     * @return 返回一个String类型的值,如果不存在该key则返回空字符串
     */
    public static String getString(Context context, String key) 
        String result = "";
        try 
            ClassLoader classLoader = context.getClassLoader();
            @SuppressWarnings("rawtypes")
            Class SystemProperties = classLoader.loadClass("android.os.SystemProperties");
            //参数类型
            @SuppressWarnings("rawtypes")
            Class[] paramTypes = new Class[1];
            paramTypes[0] = String.class;
            Method getString = SystemProperties.getMethod("get", paramTypes);
            //参数
            Object[] params = new Object[1];
            params[0] = new String(key);

            result = (String) getString.invoke(SystemProperties, params);
         catch (IllegalArgumentException e) 
            //e.printStackTrace();
            //如果key超过32个字符则抛出该异常
            Log.w(TAG, "key超过32个字符");
         catch (Exception e) 
            result = "";
        
        return result;
    

    /**
     * 根据给定的Key返回String类型的值
     *
     * @param context 上下文
     * @param key     获取指定信息所需的key
     * @param def     key不存在时的默认值
     * @return 返回一个String类型的值,如果key不存在, 并且如果def不为null则返回def,否则返回空字符串
     */
    public static String getString(Context context, String key, String def) 
        String result = def;
        try 
            ClassLoader classLoader = context.getClassLoader();
            @SuppressWarnings("rawtypes")
            Class SystemProperties = classLoader.loadClass("android.os.SystemProperties");
            //参数类型
            @SuppressWarnings("rawtypes")
            Class[] paramTypes = new Class[2];
            paramTypes[0] = String.class;
            paramTypes[1] = String.class;
            Method getString = SystemProperties.getMethod("get", paramTypes);
            //参数
            Object[] params = new Object[2];
            params[0] = new String(key);
            params[1] = new String(def);

            result = (String) getString.invoke(SystemProperties, params);
         catch (IllegalArgumentException e) 
            //e.printStackTrace();
            //如果key超过32个字符则抛出该异常
            Log.w(TAG, "key超过32个字符");
         catch (Exception e) 
            result = def;
        
        return result;
    

    /**
     * 根据给定的key返回int类型的值
     *
     * @param context 上下文
     * @param key     要查询的key
     * @param def     默认返回值
     * @return 返回一个int类型的值,如果没有发现则返回默认值 def
     */
    public static Integer getInt(Context context, String key, int def) 
        Integer result = def;
        try 
            ClassLoader classLoader = context.getClassLoader();
            @SuppressWarnings("rawtypes")
            Class SystemProperties = classLoader.loadClass("android.os.SystemProperties");
            //参数类型
            @SuppressWarnings("rawtypes")
            Class[] paramTypes = new Class[2];
            paramTypes[0] = String.class;
            paramTypes[1] = int.class;
            Method getInt = SystemProperties.getMethod("getInt", paramTypes);
            //参数
            Object[] params = new Object[2];
            params[0] = new String(key);
            params[1] = new Integer(def);
            result = (Integer) getInt.invoke(SystemProperties, params);
         catch (IllegalArgumentException e) 
            //e.printStackTrace();
            //如果key超过32个字符则抛出该异常
            Log.w(TAG, "key超过32个字符");
         catch (Exception e) 
            result = def;
        
        return result;
    

    /**
     * 根据给定的key返回long类型的值
     *
     * @param context 上下文
     * @param key     要查询的key
     * @param def     默认返回值
     * @return 返回一个long类型的值,如果没有发现则返回默认值def
     */
    public static Long getLong(Context context, String key, long def) 
        Long result = def;
        try 
            ClassLoader classLoader = context.getClassLoader();
            @SuppressWarnings("rawtypes")
            Class SystemProperties = classLoader.loadClass("android.os.SystemProperties");
            //参数类型
            @SuppressWarnings("rawtypes")
            Class[] paramTypes = new Class[2];
            paramTypes[0] = String.class;
            paramTypes[1] = long.class;
            Method getLong = SystemProperties.getMethod("getLong", paramTypes);
            //参数
            Object[] params = new Object[2];
            params[0] = new String(key);
            params[1] = new Long(def);
            result = (Long) getLong.invoke(SystemProperties, params);
         catch (IllegalArgumentException e) 
            //e.printStackTrace();
            //如果key超过32个字符则抛出该异常
            Log.w(TAG, "key超过32个字符");
         catch (Exception e) 
            result = def;
        
        return result;
    

    /**
     * 根据给定的key返回boolean类型的值
     * 如果值为'n','no','0','false' or 'off'返回false
     * 如果值为'y','yes','1','true' or 'on'返回true
     * 如果key不存在, 或者是其它的值, 则返回默认值
     *
     * @param context 上下文
     * @param key     要查询的key
     * @param def     默认返回值
     * @return 返回一个boolean类型的值,如果没有发现则返回默认值def
     */
    public static Boolean getBoolean(Context context, String key, boolean def) 
        Boolean result = def;
        try 
            ClassLoader classLoader = context.getClassLoader();
            @SuppressWarnings("rawtypes")
            Class SystemProperties = classLoader.loadClass("android.os.SystemProperties");
            //参数类型
            @SuppressWarnings("rawtypes")
            Class[] paramTypes = new Class[2];
            paramTypes[0] = String.class;
            paramTypes[1] = boolean.class;
            Method getBoolean = SystemProperties.getMethod("getBoolean", paramTypes);
            //参数
            Object[] params = new Object[2];
            params[0] = new String(key);
            params[1] = new Boolean(def);
            result = (Boolean) getBoolean.invoke(SystemProperties, params);
         catch (IllegalArgumentException e) 
            //e.printStackTrace();
            //如果key超过32个字符则抛出该异常
            Log.w(TAG, "key超过32个字符");
         catch (Exception e) 
            result = def;
        
        return result;
    

    /**
     * 根据给定的key和值设置属性, 该方法需要特定的权限才能操作.
     *
     * @param context 上下文
     * @param key     设置属性的key
     * @param val     设置属性的value
     */
    public static void set(Context context, String key, String val) 
        try 
            @SuppressWarnings("rawtypes")
            DexFile df = new DexFile(new File("/system/app/Settings.apk"));
            ClassLoader classLoader = context.getClassLoader();
            @SuppressWarnings("rawtypes")
            Class SystemProperties = Class.forName("android.os.SystemProperties");
            //参数类型
            @SuppressWarnings("rawtypes")
            Class[] paramTypes = new Class[2];
            paramTypes[0] = String.class;
            paramTypes[1] = String.class;
            Method set = SystemProperties.getMethod("set", paramTypes);
            //参数
            Object[] params = new Object[2];
            params[0] = new String(key);
            params[1] = new String(val);
            set.invoke(SystemProperties, params);
         catch (IllegalArgumentException e) 
            //e.printStackTrace();
            //如果key超过32个字符或者value超过92个字符则抛出该异常
            Log.w(TAG, "key超过32个字符或者value超过92个字符");
         catch (Exception e) 
            e.printStackTrace();
        
    




以上是关于Android通过SystemProperties获取build.prop中配置的信息的主要内容,如果未能解决你的问题,请参考以下文章

Android SystemProperties系统属性详解

Android SystemProperties系统属性详解

Android的系统属性SystemProperties

Android 反射调用SystemProperties

Android 反射调用SystemProperties

个人记录解决android studio导入SystemProperties