如何从android代码中知道路由器的IP地址?
如何从代码中找到路由器的IP地址(网关地址)?
WifiInfo.getIpAddress()
- 返回设备的 IP 地址。
在 shell 命令中“ipconfig”不返回任何值。
这是我的解决方案,但如果有更好的方法,请告诉我:
WifiManager manager = (WifiManager)getSystemService(WIFI_SERVICE);
DhcpInfo info = manager.getDhcpInfo();
info.gateway;
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
嘿,这可能对您有帮助: DHCPInfo
将以下行添加到 AndroidManifest.xml 中为了访问 wifi 功能:
由于 formatIpAddress 现在已弃用,您可以使用以下代码
Hey this might help you: DHCPInfo
Add following rows to AndroidManifest.xml in order to access wifi functionalities:
As the formatIpAddress is deprecated now you can use below code
我认为你这样做的方式是最好的(AFAIK),这里有一些来自 Cordova 插件的示例代码,它以同样的方式进行:
https://github.com/vallieres/cordova-plugin-get-router-ip-address/blob/master/src/android/GetRouterIPAddress.java
I think the way you're doing it is the best (AFAIK), here's some example code from a Cordova plugin that does it the same way:
https://github.com/vallieres/cordova-plugin-get-router-ip-address/blob/master/src/android/GetRouterIPAddress.java
试试这个:
它在我的平板电脑上使用终端模拟器运行良好!
Try this:
It worked fine in my tablet with Terminal Emulator!
要获取 IP 地址,请尝试
getInetAddress();
To get the IP address, try
getInetAddress();