SystemProperties.get/set property_get/set
在java层设置系统属性要注意几点:
1 需要有系统权限。可以在AndroidManifest.xml添加android:sharedUserId="android.uid.system",并且通过系统签名来获取系统权限。
2 需要注意有几点,key的开头必须要符合property_perms中的定义规范,如下面的定义:一定要符合prefix,uid,gid也必须要符合。如果不符合,会报错。
init: sys_prop: permission denied uid
3 需要注意 属性的name 跟 value的长度,定义的 PROP_NAME_MAX = 32 PROP_VALUE_MAX=92,但是name不能超过31,因为后面接了一位字符串结束符。
所以name不能大于31,value不能大于91。下面的代码会进行判断。否则会报错如下:
E/AndroidRuntime(15375): FATAL EXCEPTION: main
E/AndroidRuntime(15375): java.lang.IllegalArgumentException: key.length > 31
E/AndroidRuntime(15375): at android.os.SystemProperties.get(SystemProperties.java:52)
if(strlen(key) >= PROP_NAME_MAX) return -1;
if(strlen(value) >= PROP_VALUE_MAX) return -1;
4 主要sys开头的属性,重启后就没有了,persist开头的不会丢失,但是要注意name 必须以persist.sys等如下 prefix前缀打头。
5 因为SystemProperties.get/set是hide接口,所以必须要反射来调用api,或者在系统环境,或者 引用系统的class.jar包。
struct {
const char *prefix;
unsigned int uid;
unsigned int gid;
} property_perms[] = {
{ "net.rmnet0.", AID_RADIO, 0 },
{ "net.gprs.", AID_RADIO, 0 },
{ "net.ppp", AID_RADIO, 0 },
{ "net.qmi", AID_RADIO, 0 },
{ "net.lte", AID_RADIO, 0 },
{ "net.cdma", AID_RADIO, 0 },
{ "ril.", AID_RADIO, 0 },
{ "gsm.", AID_RADIO, 0 },
{ "persist.radio", AID_RADIO, 0 },
{ "net.dns", AID_RADIO, 0 },
{ "sys.usb.config", AID_RADIO, 0 },
{ "net.", AID_SYSTEM, 0 },
{ "dev.", AID_SYSTEM, 0 },
{ "runtime.", AID_SYSTEM, 0 },
{ "hw.", AID_SYSTEM, 0 },
{ "sys.", AID_SYSTEM, 0 },
{ "service.", AID_SYSTEM, 0 },
{ "wlan.", AID_SYSTEM, 0 },
{ "bluetooth.", AID_BLUETOOTH, 0 },
{ "dhcp.", AID_SYSTEM, 0 },
{ "dhcp.", AID_DHCP, 0 },
{ "debug.", AID_SYSTEM, 0 },
{ "debug.", AID_SHELL, 0 },
{ "log.", AID_SHELL, 0 },
{ "service.adb.root", AID_SHELL, 0 },
{ "service.adb.tcp.port", AID_SHELL, 0 },
{ "persist.sys.", AID_SYSTEM, 0 },
{ "persist.service.", AID_SYSTEM, 0 },
{ "persist.security.", AID_SYSTEM, 0 },
{ "persist.service.bdroid.", AID_BLUETOOTH, 0 },
{ "selinux." , AID_SYSTEM, 0 },
{ NULL, 0, 0 }
};
贴一下反射的代码。
static Method systemProperties_get = null;
static Method systemProperties_set = null;
static int sys_set(String key, String value){
int ret = -1;
if (systemProperties_set == null) {
try {
systemProperties_set = Class.forName("android.os.SystemProperties")
.getMethod("set", String.class,String.class);
} catch (Exception e) {
Log.e("TEST", "get method android.os.SystemProperties.set() error:" + e);
return -1;
}
}
if(systemProperties_set != null){
try {
Log.i("TEST", "systemProperties_set = " + systemProperties_set);
systemProperties_set.invoke(null, key,value);
} catch (Exception e) {
Log.e("TEST", "invoke method android.os.SystemProperties.set() error:" + e);
return ret;
}
}
return 0;
}
static String sys_get(String key){
String ret;
if (systemProperties_get == null) {
try {
systemProperties_get = Class.forName("android.os.SystemProperties")
.getMethod("get", String.class);
} catch (Exception e) {
Log.e("TEST", "get method android.os.SystemProperties.get() error:" + e);
return null;
}
}
try {
if ((ret = (String) systemProperties_get.invoke(null, key)) != null)
return ret;
} catch (Exception e) {
Log.e("TEST", "invoke method android.os.SystemProperties.get() error:" + e);
return null;
}
return "";
}