欢迎进入西安甲骨文培训机构 乘车路线| 关于我们| 设为首页| 加入收藏
logo 服务热线
1 2 3 3

新闻动态

联系我们

咨询报名电话:
029-85568080,029-85427081
传真:029-85568080
地址:陕西省西安市长安南路355号华银大厦(西北政法大学老校区对面西安银行南侧)

优秀学员

技术中心 当前位置:首页 > 新闻动态 > 技术中心

Android C 语言读取系统属性

点击: 时间:2014-1-4
      用 Qt Creator 写一个 Android 程序,需要读取 DNS 。C 语言怎么读呢,研究了一下,原来在 Android 的 C 库里,就有读取系统属性的方法。用 objdump 看了一下 libc.so ,找到了其中的函数。如下:

000095f0 g     F .text  00000014 __system_properties_init
00009604 g     F .text  00000014 __system_property_find
00009618 g     F .text  00000014 __system_property_find_nth
0000962c g     F .text  00000014 __system_property_get
00009640 g     F .text  00000014 __system_property_read
00009654 g     F .text  00000014 __system_property_wait



      头文件是 system_properties.h ,在 usr/include/sys目录下面。
      __system_property_get 可以用来获取一个属性值,函数原型如下:

/* Look up a system property by name, copying its value and a
** \0 terminator to the provided pointer.  The total bytes
** copied will be no greater than PROP_VALUE_MAX.  Returns
** the string length of the value.  A property that is not
** defined is identical to a property with a length 0 value.
*/
int __system_property_get(const char *name, char *value);

      读取 DNS 的代码如下:

char buf[PROP_VALUE_MAX];
__system_property_get("net.dns1", buf);
__system_property_get("net.dns2", buf);

      读取其他属性类似,设置的话可以查看 system_properties.h 看函数用法。