Android C 语言读取系统属性

本文详细介绍了如何使用C语言在Android环境下读取DNS信息。通过调用__system_property_get函数,从系统属性中获取net.dns1和net.dns2等DNS服务器地址。此方法适用于在QtCreator中开发Android应用程序。

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

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

000095f0 g     F .text 00000014 __system_properties_init00009604 g     F .text 00000014 __system_property_find00009618 g     F .text 00000014 __system_property_find_nth0000962c g     F .text 00000014 __system_property_get00009640 g     F .text 00000014 __system_property_read00009654 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 看函数用法。


           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值