当前位置:操作系统 > 安卓/Android >>

android 属性系统使用


这里说明一下具体使用场景及方法:


需求:有一个应用需要操作mtd flash,这是一层比较底级的接口操作,针对此类接口封装好了libmtd_flash.so,但是同时
碰到一个问题,应用对设备没有访问权限,多放查证增加了对sdcard的访问等还是不行,那么解决这个问题的方法就是两个:
1、在c++层做一个service,使用binder作为客户端联系桥梁
2、使用android自带的属性系统
从使用的场景及数据量,还是复杂度多方面考虑,决定采用属性系统共享数据


1、首先我们知道:
   Native代码
    当编写Native的程序时,可以使用property_get和property_set API来获得和设置属性。使用这两个API必须要包含头文件和链接libcutil库。
   Java代码
    Android在Java库中提供System.getProperty和System.setProperty方法,我们Java程序可以通过他们来设置和获得属性。
   由于我们在native层编写代码,所以使用property_get和property_set即可
  
2、列一下属性系统的接口函数


/* property_get: returns the length of the value which will never be
** greater than PROPERTY_VALUE_MAX - 1 and will always be zero terminated.
** (the length does not include the terminating zero).
**
** If the property read fails or returns an empty value, the default
** value is used (if nonnull).
*/
int property_get(const char *key, char *value, const char *default_value);


/* property_set: returns 0 on success, < 0 on failure
*/
int property_set(const char *key, const char *value);


ok,没有什么初始化或者啥的函数,直接使用set、get即可,相当方便


3、实现使用
a、直接编译一个可执行后台进程运行
b、在些中读取和设定mtd flash中数据,比如open/read/write/erase等等操作,将其数据保存到内存中,对于
   只需要get的属性数据直接从内存中查找即可
c、对于write/erase等对flash中数据改写的操作,同步内存中数据及属性系统中数据即可
d、对于上层应用或其它进程需要访问相关数据直接利用property_get/property_set即可

 

优点:一个后台进程,服务于所有客户端并保持数据一致,最重要的解决了权限问题,因为由init启动的服务进程带有root权

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,