Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 中用 C 語言讀取系統屬性

編輯:關於 Android 程式設計

用 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 中的函式用法。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved