android系統中幾種系統級別的全局變量
在android 開發中時,尤其是在開發調試系統應用的時候,有時候需要設置一個系統級別的flag標誌位,來提供給幾個應用使用判斷。例如開機完成後,或者走完開機導航後,都需要設置一個標誌位來標誌下這個狀態。
一般的話,在android 系統中常用的有以下兩種類型的標誌:
1.prop
一般的話,prop分爲好幾種,一種是系統自帶的一些,一種就是我們自己定義的。
- 系統自帶
一般這些prop的最終都會在系統的build.prop 中文件中 - 自己定義
一般的話,prop的初始化都在device 下的mk文件中配置,其中配置prop的key爲PRODUCT_PROPERTY_OVERRIDES
當然我們在真機上調試的時候可以在串口通過以下命令來讀取和設置prop
getprop key
setprop key value
一般的話我們在代碼中會通過SystemProperties 這個api來設置和讀取prop,如下:
SystemProperties.get(key,defaultValue);
SystemProperties.set(key,value);
但是呢,上面這個api一般的話只有系統應用纔可以調用(當然能調用這個的需求一般也都是在系統應用中)。但是如果是第三方應用呢,需要使用這個API怎麼辦呢?當然是牛逼的反射了。方法如下:
import android.content.Context;
import java.io.File;
import java.lang.reflect.Method;
import dalvik.system.DexFile;
public class SystemPropertiesProxy {
/**
* 根據給定Key獲取值.
*
* @return 如果不存在該key則返回空字符串
* @throws IllegalArgumentException 如果key超過32個字符則拋出該異常
*/
public static String get(Context context, String key) throws IllegalArgumentException {
String ret = "";
try {
ClassLoader cl = context.getClassLoader();
@SuppressWarnings("rawtypes")
Class SystemProperties = cl.loadClass("android.os.SystemProperties");
//參數類型
@SuppressWarnings("rawtypes")
Class[] paramTypes = new Class[1];
paramTypes[0] = String.class;
Method get = SystemProperties.getMethod("get", paramTypes);
//參數
Object[] params = new Object[1];
params[0] = new String(key);
ret = (String) get.invoke(SystemProperties, params);
} catch (IllegalArgumentException iAE) {
throw iAE;
} catch (Exception e) {
ret = "";
//TODO
}
return ret;
}
/**
* 根據Key獲取值.
*
* @return 如果key不存在, 並且如果def不爲空則返回def否則返回空字符串
* @throws IllegalArgumentException 如果key超過32個字符則拋出該異常
*/
public static String get(Context context, String key, String def) throws IllegalArgumentException {
String ret = def;
try {
ClassLoader cl = context.getClassLoader();
@SuppressWarnings("rawtypes")
Class SystemProperties = cl.loadClass("android.os.SystemProperties");
//參數類型
@SuppressWarnings("rawtypes")
Class[] paramTypes = new Class[2];
paramTypes[0] = String.class;
paramTypes[1] = String.class;
Method get = SystemProperties.getMethod("get", paramTypes);
//參數
Object[] params = new Object[2];
params[0] = new String(key);
params[1] = new String(def);
ret = (String) get.invoke(SystemProperties, params);
} catch (IllegalArgumentException iAE) {
throw iAE;
} catch (Exception e) {
ret = def;
//TODO
}
return ret;
}
/**
* 根據給定的key返回int類型值.
*
* @param key 要查詢的key
* @param def 默認返回值
* @return 返回一個int類型的值, 如果沒有發現則返回默認值
* @throws IllegalArgumentException 如果key超過32個字符則拋出該異常
*/
public static Integer getInt(Context context, String key, int def) throws IllegalArgumentException {
Integer ret = def;
try {
ClassLoader cl = context.getClassLoader();
@SuppressWarnings("rawtypes")
Class SystemProperties = cl.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);
ret = (Integer) getInt.invoke(SystemProperties, params);
} catch (IllegalArgumentException iAE) {
throw iAE;
} catch (Exception e) {
ret = def;
//TODO
}
return ret;
}
/**
* 根據給定的key返回long類型值.
*
* @param key 要查詢的key
* @param def 默認返回值
* @return 返回一個long類型的值, 如果沒有發現則返回默認值
* @throws IllegalArgumentException 如果key超過32個字符則拋出該異常
*/
public static Long getLong(Context context, String key, long def) throws IllegalArgumentException {
Long ret = def;
try {
ClassLoader cl = context.getClassLoader();
@SuppressWarnings("rawtypes")
Class SystemProperties = cl.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);
ret = (Long) getLong.invoke(SystemProperties, params);
} catch (IllegalArgumentException iAE) {
throw iAE;
} catch (Exception e) {
ret = def;
//TODO
}
return ret;
}
/**
* 根據給定的key返回boolean類型值.
* 如果值爲 'n', 'no', '0', 'false' or 'off' 返回false.
* 如果值爲'y', 'yes', '1', 'true' or 'on' 返回true.
* 如果key不存在, 或者是其它的值, 則返回默認值.
*
* @param key 要查詢的key
* @param def 默認返回值
* @return 返回一個boolean類型的值, 如果沒有發現則返回默認值
* @throws IllegalArgumentException 如果key超過32個字符則拋出該異常
*/
public static Boolean getBoolean(Context context, String key, boolean def) throws IllegalArgumentException {
Boolean ret = def;
try {
ClassLoader cl = context.getClassLoader();
@SuppressWarnings("rawtypes")
Class SystemProperties = cl.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);
ret = (Boolean) getBoolean.invoke(SystemProperties, params);
} catch (IllegalArgumentException iAE) {
throw iAE;
} catch (Exception e) {
ret = def;
//TODO
}
return ret;
}
/**
* 根據給定的key和值設置屬性, 該方法需要特定的權限才能操作.
*
* @throws IllegalArgumentException 如果key超過32個字符則拋出該異常
* @throws IllegalArgumentException 如果value超過92個字符則拋出該異常
*/
public static void set(Context context, String key, String val) throws IllegalArgumentException {
try {
@SuppressWarnings("unused")
DexFile df = new DexFile(new File("/system/app/Settings.apk"));
@SuppressWarnings("unused")
ClassLoader cl = 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 iAE) {
throw iAE;
} catch (Exception e) {
//TODO
}
}
}
2.setting
搞過系統app 尤其是setting 的同學,應該對這個很熟悉。系統setting 中的值基本都是使用的這個。比如,藍牙,wifi,亮度的默認開關值,系統默認輸入法等。
setting 主要有三種:global, system, secure
這幾個其實都是以數據庫的形式存在於系統中的,但是自從android 6.0以後這幾個表都變爲了xml文件。具體位置如下:
不同用戶放不同的路徑下,如果沒有創建新用戶,則在/data/system/users/0下
settings_global.xml, settings_system.xml, settings_secure.xml
這些值的默認值都在代碼的packages/apps/SettingsProvider/res/values/defaults.xml中
這frameworks/base/core/res/res/values/config.xml 中有一些其他配置
-
用代碼設置或者得到系統屬性的值
Settings.Secure.getInt(getContentResolver() , Settings.Secure.WIFI_ON); Settings.System.putInt(mContext.getContentResolver(), key, value);
-
用串口:(system,secure類似)
settings get global 系統屬性key settings put global 系統屬性key 系統屬性值