获取我的 wifi ip 地址 Android [英] Get my wifi ip address Android
本文介绍了获取我的 wifi ip 地址 Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何获取手机在wifi下连接时的ip地址?
How can I get the ip address of my phone when it is connected under wifi?
我在这里找到了一种方法,但它即使我在 wifi 下,也会返回类似 24.182.239.255 的内容,并且我希望返回类似 192.168.1.10 的内容.
I found a method here but it returns something like 24.182.239.255 even if I'm under wifi and I expect something like 192.168.1.10.
我想要类似的东西:
if (you are under wifi)
String ip4 = getWifiIP()
else
String ip4 = getIPAddress with the method linked before
非常感谢!
推荐答案
如果您想在连接到 Wi-Fi 时获取设备的私有 IP 地址,您可以试试这个.
If you would like to get the private IP address of your device when connected to Wi-Fi, you can try this.
WifiManager wifiMgr = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
int ip = wifiInfo.getIpAddress();
String ipAddress = Formatter.formatIpAddress(ip);
一定要加权限
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
到您的清单.
这篇关于获取我的 wifi ip 地址 Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文