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

前言

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

系统源码展示

  1. /*
  2. * Copyright (C) 2006 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package android.os;
  17. import java.util.ArrayList;
  18. /**
  19. * Gives access to the system properties store. The system properties
  20. * store contains a list of string key-value pairs.
  21. *
  22. * {@hide}
  23. */
  24. public class SystemProperties
  25. {
  26. public static final int PROP_NAME_MAX = 31;
  27. public static final int PROP_VALUE_MAX = 91;
  28. private static final ArrayList<Runnable> sChangeCallbacks = new ArrayList<Runnable>();
  29. private static native String native_get(String key);
  30. private static native String native_get(String key, String def);
  31. private static native int native_get_int(String key, int def);
  32. private static native long native_get_long(String key, long def);
  33. private static native boolean native_get_boolean(String key, boolean def);
  34. private static native void native_set(String key, String def);
  35. private static native void native_add_change_callback();
  36. /**
  37. * Get the value for the given key.
  38. * @return an empty string if the key isn't found
  39. * @throws IllegalArgumentException if the key exceeds 32 characters
  40. */
  41. public static String get(String key) {
  42. if (key.length() > PROP_NAME_MAX) {
  43. throw new IllegalArgumentException( "key.length > " + PROP_NAME_MAX);
  44. }
  45. return native_get(key);
  46. }
  47. /**
  48. * Get the value for the given key.
  49. * @return if the key isn't found, return def if it isn't null, or an empty string otherwise
  50. * @throws IllegalArgumentException if the key exceeds 32 characters
  51. */
  52. public static String get(String key, String def) {
  53. if (key.length() > PROP_NAME_MAX) {
  54. throw new IllegalArgumentException( "key.length > " + PROP_NAME_MAX);
  55. }
  56. return native_get(key, def);
  57. }
  58. /**
  59. * Get the value for the given key, and return as an integer.
  60. * @param key the key to lookup
  61. * @param def a default value to return
  62. * @return the key parsed as an integer, or def if the key isn't found or
  63. * cannot be parsed
  64. * @throws IllegalArgumentException if the key exceeds 32 characters
  65. */
  66. public static int getInt(String key, int def) {
  67. if (key.length() > PROP_NAME_MAX) {
  68. throw new IllegalArgumentException( "key.length > " + PROP_NAME_MAX);
  69. }
  70. return native_get_int(key, def);
  71. }
  72. /**
  73. * Get the value for the given key, and return as a long.
  74. * @param key the key to lookup
  75. * @param def a default value to return
  76. * @return the key parsed as a long, or def if the key isn't found or
  77. * cannot be parsed
  78. * @throws IllegalArgumentException if the key exceeds 32 characters
  79. */
  80. public static long getLong(String key, long def) {
  81. if (key.length() > PROP_NAME_MAX) {
  82. throw new IllegalArgumentException( "key.length > " + PROP_NAME_MAX);
  83. }
  84. return native_get_long(key, def);
  85. }
  86. /**
  87. * Get the value for the given key, returned as a boolean.
  88. * Values 'n', 'no', '0', 'false' or 'off' are considered false.
  89. * Values 'y', 'yes', '1', 'true' or 'on' are considered true.
  90. * (case sensitive).
  91. * If the key does not exist, or has any other value, then the default
  92. * result is returned.
  93. * @param key the key to lookup
  94. * @param def a default value to return
  95. * @return the key parsed as a boolean, or def if the key isn't found or is
  96. * not able to be parsed as a boolean.
  97. * @throws IllegalArgumentException if the key exceeds 32 characters
  98. */
  99. public static boolean getBoolean(String key, boolean def) {
  100. if (key.length() > PROP_NAME_MAX) {
  101. throw new IllegalArgumentException( "key.length > " + PROP_NAME_MAX);
  102. }
  103. return native_get_boolean(key, def);
  104. }
  105. /**
  106. * Set the value for the given key.
  107. * @throws IllegalArgumentException if the key exceeds 32 characters
  108. * @throws IllegalArgumentException if the value exceeds 92 characters
  109. */
  110. public static void set(String key, String val) {
  111. if (key.length() > PROP_NAME_MAX) {
  112. throw new IllegalArgumentException( "key.length > " + PROP_NAME_MAX);
  113. }
  114. if (val != null && val.length() > PROP_VALUE_MAX) {
  115. throw new IllegalArgumentException( "val.length > " +
  116. PROP_VALUE_MAX);
  117. }
  118. native_set(key, val);
  119. }
  120. public static void addChangeCallback(Runnable callback) {
  121. synchronized (sChangeCallbacks) {
  122. if (sChangeCallbacks.size() == 0) {
  123. native_add_change_callback();
  124. }
  125. sChangeCallbacks.add(callback);
  126. }
  127. }
  128. static void callChangeCallbacks() {
  129. synchronized (sChangeCallbacks) {
  130. //Log.i("foo", "Calling " + sChangeCallbacks.size() + " change callbacks!");
  131. if (sChangeCallbacks.size() == 0) {
  132. return;
  133. }
  134. ArrayList<Runnable> callbacks = new ArrayList<Runnable>(sChangeCallbacks);
  135. for ( int i= 0; i<callbacks.size(); i++) {
  136. callbacks.get(i).run();
  137. }
  138. }
  139. }
  140. }

工具类源码展示

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

  1. package com.mufeng.testproject.tools;
  2. import android.content.Context;
  3. import android.util.Log;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.lang.reflect.Method;
  7. import dalvik.system.DexFile;
  8. /**
  9. * Created by zhangqing on 2017/3/1.
  10. */
  11. public class SystemPropertiesProxy {
  12. public static final String TAG = "SystemPropertiesProxy";
  13. /**
  14. * 根据给定的Key返回String类型的值
  15. *
  16. * @param context 上下文
  17. * @param key 获取指定信息所需的key
  18. * @return 返回一个String类型的值,如果不存在该key则返回空字符串
  19. */
  20. public static String getString(Context context, String key) {
  21. String result = "";
  22. try {
  23. ClassLoader classLoader = context.getClassLoader();
  24. @SuppressWarnings( "rawtypes")
  25. Class SystemProperties = classLoader.loadClass( "android.os.SystemProperties");
  26. //参数类型
  27. @SuppressWarnings( "rawtypes")
  28. Class[] paramTypes = new Class[ 1];
  29. paramTypes[ 0] = String.class;
  30. Method getString = SystemProperties.getMethod( "get", paramTypes);
  31. //参数
  32. Object[] params = new Object[ 1];
  33. params[ 0] = new String(key);
  34. result = (String) getString.invoke(SystemProperties, params);
  35. } catch (IllegalArgumentException e) {
  36. //e.printStackTrace();
  37. //如果key超过32个字符则抛出该异常
  38. Log.w(TAG, "key超过32个字符");
  39. } catch (Exception e) {
  40. result = "";
  41. }
  42. return result;
  43. }
  44. /**
  45. * 根据给定的Key返回String类型的值
  46. *
  47. * @param context 上下文
  48. * @param key 获取指定信息所需的key
  49. * @param def key不存在时的默认值
  50. * @return 返回一个String类型的值,如果key不存在, 并且如果def不为null则返回def,否则返回空字符串
  51. */
  52. public static String getString(Context context, String key, String def) {
  53. String result = def;
  54. try {
  55. ClassLoader classLoader = context.getClassLoader();
  56. @SuppressWarnings( "rawtypes")
  57. Class SystemProperties = classLoader.loadClass( "android.os.SystemProperties");
  58. //参数类型
  59. @SuppressWarnings( "rawtypes")
  60. Class[] paramTypes = new Class[ 2];
  61. paramTypes[ 0] = String.class;
  62. paramTypes[ 1] = String.class;
  63. Method getString = SystemProperties.getMethod( "get", paramTypes);
  64. //参数
  65. Object[] params = new Object[ 2];
  66. params[ 0] = new String(key);
  67. params[ 1] = new String(def);
  68. result = (String) getString.invoke(SystemProperties, params);
  69. } catch (IllegalArgumentException e) {
  70. //e.printStackTrace();
  71. //如果key超过32个字符则抛出该异常
  72. Log.w(TAG, "key超过32个字符");
  73. } catch (Exception e) {
  74. result = def;
  75. }
  76. return result;
  77. }
  78. /**
  79. * 根据给定的key返回int类型的值
  80. *
  81. * @param context 上下文
  82. * @param key 要查询的key
  83. * @param def 默认返回值
  84. * @return 返回一个int类型的值,如果没有发现则返回默认值 def
  85. */
  86. public static Integer getInt(Context context, String key, int def) {
  87. Integer result = def;
  88. try {
  89. ClassLoader classLoader = context.getClassLoader();
  90. @SuppressWarnings( "rawtypes")
  91. Class SystemProperties = classLoader.loadClass( "android.os.SystemProperties");
  92. //参数类型
  93. @SuppressWarnings( "rawtypes")
  94. Class[] paramTypes = new Class[ 2];
  95. paramTypes[ 0] = String.class;
  96. paramTypes[ 1] = int.class;
  97. Method getInt = SystemProperties.getMethod( "getInt", paramTypes);
  98. //参数
  99. Object[] params = new Object[ 2];
  100. params[ 0] = new String(key);
  101. params[ 1] = new Integer(def);
  102. result = (Integer) getInt.invoke(SystemProperties, params);
  103. } catch (IllegalArgumentException e) {
  104. //e.printStackTrace();
  105. //如果key超过32个字符则抛出该异常
  106. Log.w(TAG, "key超过32个字符");
  107. } catch (Exception e) {
  108. result = def;
  109. }
  110. return result;
  111. }
  112. /**
  113. * 根据给定的key返回long类型的值
  114. *
  115. * @param context 上下文
  116. * @param key 要查询的key
  117. * @param def 默认返回值
  118. * @return 返回一个long类型的值,如果没有发现则返回默认值def
  119. */
  120. public static Long getLong(Context context, String key, long def) {
  121. Long result = def;
  122. try {
  123. ClassLoader classLoader = context.getClassLoader();
  124. @SuppressWarnings( "rawtypes")
  125. Class SystemProperties = classLoader.loadClass( "android.os.SystemProperties");
  126. //参数类型
  127. @SuppressWarnings( "rawtypes")
  128. Class[] paramTypes = new Class[ 2];
  129. paramTypes[ 0] = String.class;
  130. paramTypes[ 1] = long.class;
  131. Method getLong = SystemProperties.getMethod( "getLong", paramTypes);
  132. //参数
  133. Object[] params = new Object[ 2];
  134. params[ 0] = new String(key);
  135. params[ 1] = new Long(def);
  136. result = (Long) getLong.invoke(SystemProperties, params);
  137. } catch (IllegalArgumentException e) {
  138. //e.printStackTrace();
  139. //如果key超过32个字符则抛出该异常
  140. Log.w(TAG, "key超过32个字符");
  141. } catch (Exception e) {
  142. result = def;
  143. }
  144. return result;
  145. }
  146. /**
  147. * 根据给定的key返回boolean类型的值
  148. * 如果值为'n','no','0','false' or 'off'返回false
  149. * 如果值为'y','yes','1','true' or 'on'返回true
  150. * 如果key不存在, 或者是其它的值, 则返回默认值
  151. *
  152. * @param context 上下文
  153. * @param key 要查询的key
  154. * @param def 默认返回值
  155. * @return 返回一个boolean类型的值,如果没有发现则返回默认值def
  156. */
  157. public static Boolean getBoolean(Context context, String key, boolean def) {
  158. Boolean result = def;
  159. try {
  160. ClassLoader classLoader = context.getClassLoader();
  161. @SuppressWarnings( "rawtypes")
  162. Class SystemProperties = classLoader.loadClass( "android.os.SystemProperties");
  163. //参数类型
  164. @SuppressWarnings( "rawtypes")
  165. Class[] paramTypes = new Class[ 2];
  166. paramTypes[ 0] = String.class;
  167. paramTypes[ 1] = boolean.class;
  168. Method getBoolean = SystemProperties.getMethod( "getBoolean", paramTypes);
  169. //参数
  170. Object[] params = new Object[ 2];
  171. params[ 0] = new String(key);
  172. params[ 1] = new Boolean(def);
  173. result = (Boolean) getBoolean.invoke(SystemProperties, params);
  174. } catch (IllegalArgumentException e) {
  175. //e.printStackTrace();
  176. //如果key超过32个字符则抛出该异常
  177. Log.w(TAG, "key超过32个字符");
  178. } catch (Exception e) {
  179. result = def;
  180. }
  181. return result;
  182. }
  183. /**
  184. * 根据给定的key和值设置属性, 该方法需要特定的权限才能操作.
  185. *
  186. * @param context 上下文
  187. * @param key 设置属性的key
  188. * @param val 设置属性的value
  189. */
  190. public static void set(Context context, String key, String val) {
  191. try {
  192. @SuppressWarnings( "rawtypes")
  193. DexFile df = new DexFile( new File( "/system/app/Settings.apk"));
  194. ClassLoader classLoader = context.getClassLoader();
  195. @SuppressWarnings( "rawtypes")
  196. Class SystemProperties = Class.forName( "android.os.SystemProperties");
  197. //参数类型
  198. @SuppressWarnings( "rawtypes")
  199. Class[] paramTypes = new Class[ 2];
  200. paramTypes[ 0] = String.class;
  201. paramTypes[ 1] = String.class;
  202. Method set = SystemProperties.getMethod( "set", paramTypes);
  203. //参数
  204. Object[] params = new Object[ 2];
  205. params[ 0] = new String(key);
  206. params[ 1] = new String(val);
  207. set.invoke(SystemProperties, params);
  208. } catch (IllegalArgumentException e) {
  209. //e.printStackTrace();
  210. //如果key超过32个字符或者value超过92个字符则抛出该异常
  211. Log.w(TAG, "key超过32个字符或者value超过92个字符");
  212. } catch (Exception e) {
  213. e.printStackTrace();
  214. }
  215. }
  216. }

猜你喜欢

转载自blog.csdn.net/xiaozhude/article/details/80915356
今日推荐