android 获取dns地址吗,Android: 获取dns IP地址

在Android系统中,想获取dns server的IP地址并不是一件容易的事,在bionic中,有关于获取dns IP地址的相关代码,但是你在native代码中却不能接调用相关的函数获取。

如在bionic/libc/include/resolv.h中有如下声明:

__BEGIN_DECLS

#pragma GCC visibility push(default)

struct res_state;

extern struct __res_state *__res_state(void);

#define _res (*__res_state())

#define b64_ntop __b64_ntop

#define b64_pton __b64_pton

extern int b64_ntop(u_char const*, size_t, char*, size_t);

extern int b64_pton(char const*, u_char*, size_t);

#define dn_comp __dn_comp

extern int dn_comp(const char*, u_char*, int, u_char**, u_char**);

extern int dn_expand(const u_char*, const u_char*, const u_char*, char*, int);

#pragma GCC visibility pop

__END_DECLS

文件中设置了编译条件:visibility为default, 而在bionic/libc/Android.mk文件中,将visibility设置为了hidden, 就是使得你在native代码中使用了_res或者是__res_state()函数时,在进行编译链接时,会报如下错误:

: error: undefined reference to '__res_state'

collect2: error: ld returned 1 exit status

既然这条路行不通,那我们还有什么别的办法吗,先看一下与dns相关的系统属性:

$ adb shell getprop | grep dns

[dhcp.wlan0.dns1]: [10.0.0.1]

[dhcp.wlan0.dns2]: []

[dhcp.wlan0.dns3]: []

[dhcp.wlan0.dns4]: []

[net.change]: [net.dns1]

[net.dns1]: [10.0.0.1]

这里我们可以看到当前连接的wifi网络的dns以及系统全局使用的dns(net.dns1),也许,我们只能从系统属性获取dns了。

在java代码中,我们可以通过LinkProperties即调用ConnectivityManager.getLinkProperties()得到特定网络的相关信息,当然这其中也包含了dns server的信息(LinkProperties.getDnsServers()):

Describes the properties of a network link. A link represents a connection to a network. It may have multiple addresses and multiple gateways, multiple dns servers but only one http proxy and one network interface. Note that this is just a holder of data. Modifying it does not affect live networks.

相关的代码如下:

// ...

ConnectivityManager cm = (ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE);

NetworkInfo activeNetworkInfo = cm.getActiveNetworkInfo();

for (Network network : cm.getAllNetworks()) {

NetworkInfo networkInfo = cm.getNetworkInfo(network);

if (networkInfo.getType() == activeNetworkInfo.getType()) {

LinkProperties lp = cm.getLinkProperties(network);

for (InetAddress addr : lp.getDnsServers()) {

// Get DNS IP address here:

}

}

}

// ...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个获取以太网信息的示例代码: ``` public class EthernetInfoActivity extends AppCompatActivity { private TextView tvIpMode; private TextView tvIpAddress; private TextView tvGateway; private TextView tvSubnetMask; private TextView tvDns1; private TextView tvDns2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ethernet_info); tvIpMode = findViewById(R.id.tv_ip_mode); tvIpAddress = findViewById(R.id.tv_ip_address); tvGateway = findViewById(R.id.tv_gateway); tvSubnetMask = findViewById(R.id.tv_subnet_mask); tvDns1 = findViewById(R.id.tv_dns1); tvDns2 = findViewById(R.id.tv_dns2); // 获取以太网管理器 EthernetManager ethernetManager = (EthernetManager) getSystemService(Context.ETHERNET_SERVICE); // 获取IP模式 int ipMode = ethernetManager.getEthernetMode(); if (ipMode == EthernetManager.ETHERNET_CONNECT_MODE_DHCP) { tvIpMode.setText("DHCP"); } else if (ipMode == EthernetManager.ETHERNET_CONNECT_MODE_MANUAL) { tvIpMode.setText("静态IP"); } // 获取IP地址、网关、子网掩码、DNS EthernetDevInfo devInfo = ethernetManager.getSavedEthernetDevInfo(); if (devInfo != null) { tvIpAddress.setText(devInfo.getIpAddress()); tvGateway.setText(devInfo.getRouteAddr()); tvSubnetMask.setText(devInfo.getNetMask()); tvDns1.setText(devInfo.getDnsAddr()); tvDns2.setText(devInfo.getDns2Addr()); } } } ``` 在布局文件中,我们需要添加相应的TextView: ``` <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="16dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="IP模式:" /> <TextView android:id="@+id/tv_ip_mode" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="16dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="IP地址:" /> <TextView android:id="@+id/tv_ip_address" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="16dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="网关地址:" /> <TextView android:id="@+id/tv_gateway" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="16dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="子网掩码:" /> <TextView android:id="@+id/tv_subnet_mask" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="16dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="首选DNS:" /> <TextView android:id="@+id/tv_dns1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="16dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="备选DNS:" /> <TextView android:id="@+id/tv_dns2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> ``` 运行该示例代码将会显示以太网的IP模式、IP地址、网关地址、子网掩码、首选DNS、备选DNS
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值