如何知道从code在Android上的路由器的IP地址? [英] how to know ip address of the router from code in android?

查看:218
本文介绍了如何知道从code在Android上的路由器的IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你怎么能找到路由器(网关地址)为code的IP地址?

WifiInfo.getIpAddress() - 返回设备的IP地址

在一个shell命令的IPCONFIG的不返回任何值。

下面是我的解决方案,但是请让我知道是否有更好的方法来做到这一点:

  WifiManager经理=(WifiManager)getSystemService(WIFI_SERVICE);
DhcpInfo信息= manager.getDhcpInfo();
info.gateway;
 

解决方案

嘿,这可能会帮助您: DHCPInfo

 最后WifiManager经理=(WifiManager)super.getSystemService(WIFI_SERVICE);
最终DhcpInfo DHCP = manager.getDhcpInfo();
最终的字符串地址= Formatter.formatIpAddress(dhcp.gateway);
 

添加以下行的Andr​​oidManifest.xml才能访问无线网络的功能:

 <使用-权限的Andr​​oid:名称=android.permission.ACCESS_WIFI_STATE/>
<使用-权限的Andr​​oid:名称=android.permission.CHANGE_WIFI_STATE/>
 

How can you find the IP address of the router (gateway address) from code?

WifiInfo.getIpAddress() - returns IP address of device.

In a shell command "ipconfig" does not return any value.

Here is my solution, but please let me know if there is a better way to do this:

WifiManager manager = (WifiManager)getSystemService(WIFI_SERVICE);
DhcpInfo info = manager.getDhcpInfo();
info.gateway;

解决方案

Hey this might help you: DHCPInfo

final WifiManager manager = (WifiManager) super.getSystemService(WIFI_SERVICE);
final DhcpInfo dhcp = manager.getDhcpInfo();
final String address = Formatter.formatIpAddress(dhcp.gateway);

Add following rows to AndroidManifest.xml in order to access wifi functionalities:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

这篇关于如何知道从code在Android上的路由器的IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆