Android(安卓)属性: persist
转自:http://blog.csdn.net/cloudwu007/article/details/7850496
问题:发现adb sehll setProp所设属性值在下次重起后被清除
adb shell setprop testing.mediascanner.skiplist /storage/sdcard1/test
结论:1.必须采用persist.开头的属性名才能永久保存。
2.如果具有root权限,可以直接编辑/system/build.prop并加入需要永久保存的属性
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 plain copy print ?
- publicstaticStringget(Stringkey,Stringdef){}
- publicstaticvoidset(Stringkey,Stringval){}
The Native API:
[cpp] view plain copy print ?- intproperty_get(constchar*key,char*value,constchar*default_value);
- intproperty_set(constchar*key,constchar*value);
bionic/libc/include/sys/system_properties.h
[cpp] view plain copy print ?
- #definePROP_NAME_MAX32
- #definePROP_VALUE_MAX92
bionic/libc/include/sys/_system_properties.h
[cpp] view plain copy print ?
- #definePROP_PATH_RAMDISK_DEFAULT"/default.prop"
- #definePROP_PATH_SYSTEM_BUILD"/system/build.prop"
- #definePROP_PATH_SYSTEM_DEFAULT"/system/default.prop"
- #definePROP_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] view plain copy print ?
- voidstart_property_service(void)
- {
- intfd;
- load_properties_from_file(PROP_PATH_SYSTEM_BUILD);
- load_properties_from_file(PROP_PATH_SYSTEM_DEFAULT);
- #ifdefALLOW_LOCAL_PROP_OVERRIDE
- load_properties_from_file(PROP_PATH_LOCAL_OVERRIDE);
- #endif/*ALLOW_LOCAL_PROP_OVERRIDE*/
- /*Readpersistentpropertiesafteralldefaultvalueshavebeenloaded.*/
- load_persistent_properties();
- update_legacy_atvc_properties();
设置属性,ro.开头的属性将不能被更改属性值,persist.开头的属性会被永久纪录,其他属性值在重新开机后均将被丢弃:
[cpp] view plain copy print ?
- intproperty_set(constchar*name,constchar*value)
- {
- if(namelen<1)return-1;
- pi=(prop_info*)__system_property_find(name);
- if(pi!=0){
- /*ro.*propertiesmayNEVERbemodifiedonceset*/
- 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);
- }
- /*Ifnamestartswith"net."treatasaDNSproperty.*/
- if(strncmp("net.",name,strlen("net."))==0){
- if(strcmp("net.change",name)==0){
- return0;
- }
- /*
- *The'net.change'propertyisaspecialpropertyusedtrackwhenany
- *'net.*'propertynameisupdated.Itis_ONLY_updatedhere.Itsvalue
- *containsthelastupdated'net.*'property.
- */
- property_set("net.change",name);
- }elseif(persistent_properties_loaded&&
- strncmp("persist.",name,strlen("persist."))==0){
- /*
- *Don'twritepropertiestodiskuntilafterwehavereadalldefaultproperties
- *topreventthemfrombeingoverwrittenbydefaultvalues.
- */
- write_persistent_property(name,value);
- }
- property_changed(name,value);
- return0;
- }
当用户设置属性时,如果以属性名字以persist.开头,则会同时在/data/property目录下创建以该属性名字命名的文件,并写入属性值。
[cpp] view plain copy print ?- #definePERSISTENT_PROPERTY_DIR"/data/property"
- staticvoidwrite_persistent_property(constchar*name,constchar*value)
- {
- constchar*tempPath=PERSISTENT_PROPERTY_DIR"/.temp";
- charpath[PATH_MAX];
- intfd,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("Unabletowritepersistentpropertytotempfile%serrno:%d\n",tempPath,errno);
- return;
- }
- write(fd,value,strlen(value));
- close(fd);
- if(rename(tempPath,path)){
- unlink(tempPath);
- ERROR("Unabletorenamepersistentpropertyfile%sto%s\n",tempPath,path);
- }
- }
加载永久属性时,会读入在目录/data/property下所有名字以persist.开头的文件内容,作为该名字对应的属性值。
[cpp] view plain copy print ?- staticvoidload_persistent_properties()
- {
- DIR*dir=opendir(PERSISTENT_PROPERTY_DIR);
- ...
- if(dir){
- while((entry=readdir(dir))!=NULL){
- if(strncmp("persist.",entry->d_name,strlen("persist.")))
- continue;
ref:
1.Android Property Systemhttp://blog.csdn.net/magod/article/details/7255217
更多相关文章
- Android开发 常用控件罕见特殊属性集锦
- android 让ImageView的图片全屏填充
- 在android style.xml文件中使用自定义属性
- Android——Intent简介
- Android使用ListView应该注意的地方
- String.xml中product属性使用
- Android(安卓)UI--动画 Animation
- android statusbar隐藏的几种方式
- Android系统启动分析(Init->Zygote->SystemServer->Home activit