代码之家  ›  专栏  ›  技术社区  ›  rayman

获取我手机的内部IP

  •  -1
  • rayman  · 技术社区  · 14 年前

    我的电话有IP。 我想知道我怎样才能取回它? 在网上搜索之后,我发现我只能得到当前的外部IP(我想要我手机的本地永久IP)。

    谢谢,

    雷。

    3 回复  |  直到 13 年前
        1
  •  10
  •   RubenGM Kjetil Watnedal    13 年前

    谷歌上的一个快速搜索把我送到了这里: 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();
    

    编辑 : Emulator似乎在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);
    
        2
  •  3
  •   stack72    14 年前

    你的IP会随着你连接的每一个网络而改变-你的手机有一个MAC地址-这就是你想要找到的吗?

        3
  •  1
  •   DonGru    14 年前

    我可以称之为“本地永久”的IP是 localhost IP始终是 127.0.0.1

    任何其他网络适配器(在本例中为WiFi)的IP根据网络而变化