拿我的手机的内部IP

ray*_*man -1 android

我的手机有IP.我想知道如何找回它?在网上搜索后,我发现我只能获得当前的外部IP.(我想要我手机的本地 - 烫发ip)

谢谢,

射线.

Kje*_*dal 10

谷歌快速搜索发送给我:http: //www.droidnova.com/get-the-ip-address-of-your-device,304.html

阅读有关如何使用第一个代码块获取wifi IP地址的评论(在本地网络上,而不是公共IP)

WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
Run Code Online (Sandbox Code Playgroud)

编辑:模拟器似乎在wifiInfo.getIpAddress()上返回0,但它在手机上工作正常.以下代码将整数转换为ip地址:

String ipBinary = Integer.toBinaryString(ipAddress);

//Leading zeroes are removed by toBinaryString, this will add them back.
while(ipBinary.length() < 32) {
    ipBinary = "0" + ipBinary;
}

//get the four different parts
String a=ipBinary.substring(0,8);
String b=ipBinary.substring(8,16);
String c=ipBinary.substring(16,24);
String d=ipBinary.substring(24,32);

//Convert to numbers
String actualIpAddress =Integer.parseInt(d,2)+"."+Integer.parseInt(c,2)+"."+Integer.parseInt(b,2)+"."+Integer.parseInt(a,2);
Run Code Online (Sandbox Code Playgroud)

  • 据我了解,它将返回内部IP地址.阅读"Erren于2009年9月8日发表的评论".似乎他得到了内部IP地址,但是后退或者其他东西.他写道"因为我的wlan IP地址是10.83.35.80",他回来的地址是80.35.83.10.我没有开发环境,所以我自己无法检查. (2认同)