Android属性:所设属性值为何在重起后被清除
来源:互联网 发布:什么是矩阵的秩定义 编辑:程序博客网 时间:2024/06/05 05:28
问题:发现adb sehll setProp所设属性值在下次重起后被清除
adb shell setprop testing.mediascanner.skiplist /storage/sdcard1/test
结论:必须采用persist.开头的属性名才能永久保存。
On system initialization, Android will allocates a block of shared memory for storing the properties. This is done in “init” daemon whose sourcecode is at: device/system/init. The “init” daemon will start a PropertyService.
The Property Service is running in the process of “init”daemon. Every client that wants to SET property needs to connect to theProperty Service and send message to Property Service. Property Servicewill update/create the property in shared memory. Any client that wants to GET property can read the property from the shared memory directly.This promotes the read performance.
Java API:
import android.os.SystemProperties;
[javascript] view plaincopyprint?
public static String get(String key, String def) {}
public static void set(String key, String val) {}
The Native API:
[cpp]
int property_get(const char *key, char *value, const char *default_value);
int property_set(const char *key, const char *value);
bionic/libc/include/sys/system_properties.h
[cpp] view plaincopyprint?
#define PROP_NAME_MAX 32
#define PROP_VALUE_MAX 92
以上定义可知属性最大长度为32,属性值最大长度92字节.
bionic/libc/include/sys/_system_properties.h
[cpp]
#define PROP_PATH_RAMDISK_DEFAULT "/default.prop"
#define PROP_PATH_SYSTEM_BUILD "/system/build.prop"
#define PROP_PATH_SYSTEM_DEFAULT "/system/default.prop"
#define PROP_PATH_LOCAL_OVERRIDE "/data/local.prop"
属性服务启动后会从系统文件中读取默认的属性,并写入共享内存中,以下4个文件为按顺序读取:
/default.prop
/system/build.prop
/system/default.prop
/data/local.prop
后读入的属性将覆盖前面读取的相同的属性。
system/core/init/property_service.c
[cpp]
void start_property_service(void)
{
int fd;
load_properties_from_file(PROP_PATH_SYSTEM_BUILD);
load_properties_from_file(PROP_PATH_SYSTEM_DEFAULT);
#ifdef ALLOW_LOCAL_PROP_OVERRIDE
load_properties_from_file(PROP_PATH_LOCAL_OVERRIDE);
#endif /* ALLOW_LOCAL_PROP_OVERRIDE */
/* Read persistent properties after all default values have been loaded. */
load_persistent_properties();
update_legacy_atvc_properties();
设置属性,ro.开头的属性将不能被更改属性值,persist.开头的属性会被永久纪录,其他属性值在重新开机后均将被丢弃:
[cpp]
int property_set(const char *name, const char *value)
{
if(namelen < 1) return -1;
pi = (prop_info*) __system_property_find(name);
if(pi != 0) {
/* ro.* properties may NEVER be modified once set */
if(!strncmp(name, "ro.", 3)) return -1;
pa = __system_property_area__;
update_prop_info(pi, value, valuelen);
pa->serial++;
__futex_wake(&pa->serial, INT32_MAX);
} else {
pa = __system_property_area__;
if(pa->count == PA_COUNT_MAX) return -1;
pi = pa_info_array + pa->count;
pi->serial = (valuelen << 24);
memcpy(pi->name, name, namelen + 1);
memcpy(pi->value, value, valuelen + 1);
pa->toc[pa->count] =
(namelen << 24) | (((unsigned) pi) - ((unsigned) pa));
pa->count++;
pa->serial++;
__futex_wake(&pa->serial, INT32_MAX);
}
/* If name starts with "net." treat as a DNS property. */
if (strncmp("net.", name, strlen("net.")) == 0) {
if (strcmp("net.change", name) == 0) {
return 0;
}
/*
* The 'net.change' property is a special property used track when any
* 'net.*' property name is updated. It is _ONLY_ updated here. Its value
* contains the last updated 'net.*' property.
*/
property_set("net.change", name);
} else if (persistent_properties_loaded &&
strncmp("persist.", name, strlen("persist.")) == 0) {
/*
* Don't write properties to disk until after we have read all default properties
* to prevent them from being overwritten by default values.
*/
write_persistent_property(name, value);
}
property_changed(name, value);
return 0;
}
当用户设置属性时,如果以属性名字以persist.开头,则会同时在/data/property目录下创建以该属性名字命名的文件,并写入属性值。
[cpp]
#define PERSISTENT_PROPERTY_DIR "/data/property"
static void write_persistent_property(const char *name, const char *value)
{
const char *tempPath = PERSISTENT_PROPERTY_DIR "/.temp";
char path[PATH_MAX];
int fd, length;
snprintf(path, sizeof(path), "%s/%s", PERSISTENT_PROPERTY_DIR, name);
fd = open(tempPath, O_WRONLY|O_CREAT|O_TRUNC, 0600);
if (fd < 0) {
ERROR("Unable to write persistent property to temp file %s errno: %d\n", tempPath, errno);
return;
}
write(fd, value, strlen(value));
close(fd);
if (rename(tempPath, path)) {
unlink(tempPath);
ERROR("Unable to rename persistent property file %s to %s\n", tempPath, path);
}
}
加载永久属性时,会读入在目录/data/property下所有名字以persist.开头的文件内容,作为该名字对应的属性值。
[cpp]
static void load_persistent_properties()
{
DIR* dir = opendir(PERSISTENT_PROPERTY_DIR);
...
if (dir) { www.2cto.com
while ((entry = readdir(dir)) != NULL) {
if (strncmp("persist.", entry->d_name, strlen("persist.")))
continue;
作者:cloudwu007
- Android属性:所设属性值为何在重起后被清除
- Android属性:所设属性值为何在重起后被清除
- Android属性:所设属性值为何在重起后被清除
- 暗藏玄机为何CSS所定义属性没有效果
- TImge属性Stretch属性设为True时不能清除图像
- java发射机制获取属性,给属性设值
- 【WPF】在设计时,属性设置错误弹出提示框
- 在代码中设ImageView的src属性
- 自定义UserControl的属性为什么不能在设计时显示在属性窗口中
- 清除HTML file控件value属性值
- 清除<li>属性
- 设计时属性:设计时属性相关类型
- 组件的设计时属性
- 02_Spring3.2_属性类型设值注入
- Java反射获取对象属性并设值
- 根据属性名称获取其在对象中所存放的值
- 属性在viewDidUnload中设nil,在dealloc中却release
- 清除css的display属性
- Xcode5 运行程序 提示IOS 模拟器未能安装此应用程序
- OCS部署
- 【小松教你手游开发】【unity实用技能】Unity3D研究院之为什么Inspector视图中脚本前面的勾选框没了
- 关于Git的一点学习(二)
- UItextField 文字变化时的监听设置
- Android属性:所设属性值为何在重起后被清除
- uva125(floyd)
- POJ3320 Jessica's Reading Problem(双指针)
- Android 开源库——侧滑菜单栏(SlidingMenu)的导入和使用
- 判断NSString 是否为数字
- gps 数据解析-NMEA 0183协议
- HDU 1058 Humble Numbers
- hdu 2542 补兵(二分,模拟)
- Eclipse上搭建SSH(struts-2.2.3 + spring-2.5.6 + hibernate-3.6.8)框架-附源码