SystemProperties在android系统里面使用的很广泛,用来保存一些简单的数据,使用起来非常简单
SystemProperties.set("persist.test.flycom", "1");
SystemProperties.get("persist.test.flycom", "2");
还可以获取Boolean,Int,Long值
/**
* 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
*/
public static int getInt(String key, int def) {
if (TRACK_KEY_ACCESS) onKeyAccess(key);
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
*/
public static long getLong(String key, long def) {
if (TRACK_KEY_ACCESS) onKeyAccess(key);
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.
*/
public static boolean getBoolean(String key, boolean def) {
if (TRACK_KEY_ACCESS) onKeyAccess(key);
return native_get_boolean(key, def);
}
这里特别说明一下权限问题,如果没有权限的话,会出现下面的错误:
07-05 03:24:48.541 6328-6350/? E/AndroidRuntime: *** FATAL EXCEPTION IN SYSTEM PROCESS: android.display
java.lang.RuntimeException: failed to set system property
at android.os.SystemProperties.native_set(Native Method)
at android.os.SystemProperties.set(SystemProperties.java:155)
at com.android.server.am.ActivityManagerService.finishBooting(ActivityManagerService.java:7504)
at com.android.server.am.ActivityManagerService.bootAnimationComplete(ActivityManagerService.java:7527)
at com.android.server.wm.WindowManagerService.performEnableScreen(WindowManagerService.java:3621)
at com.android.server.wm.WindowManagerService.-wrap6(Unknown Source:0)
at com.android.server.wm.WindowManagerService$H.handleMessage(WindowManagerService.java:5349)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.os.HandlerThread.run(HandlerThread.java:65)
at com.android.server.ServiceThread.run(ServiceThread.java:46)
如果是在app里面使用,只需要在device\mediatek\sepolicy\bsp\non_plat\property_contexts里面添加
persist.test.flycom u:object_r:system_prop:s0
而且app必须要有系统的签名 android:sharedUserId="android.uid.system"。不是在系统里面开发的话,可以使用反射来调用,
public public String getProperty(String key, String defaultValue) {
String value = defaultValue;
try {
Class<?> c = Class.forName("android.os.SystemProperties");
Method get = c.getMethod("get", String.class, String.class);
value = (String)(get.invoke(c, key, "unknown" ));
} catch (Exception e) {
e.printStackTrace();
}finally {
return value;
}
}
public void setProperty(String key, String value) {
try {
Class<?> c = Class.forName("android.os.SystemProperties");
Method set = c.getMethod("set", String.class, String.class);
set.invoke(c, key, value );
} catch (Exception e) {
e.printStackTrace();
}
}
如果是在framework层使用,需要修改3个文件,
device/mediatek/sepolicy/bsp/non_plat/property.te
type flycom_test_prop, property_type, mtk_core_property_type;
device/mediatek/sepolicy/bsp/non_plat/property_contexts
persist.test.flycom u:object_r:flycom_test_prop:s0
device/mediatek/sepolicy/bsp/non_plat/system_server.te
allow system_server flycom_test_prop:property_service set;
当然也可以使用一些android自带的属性,以sys、dev、persist.sys开头的,默认是有权限的,不需要添加。