2010-11-11 69 views

回答

3

我一直在这个在我的书签了一段时间,但从来没有对它进行测试:

http://www.droidnova.com/get-the-ip-address-of-your-device,304.html

public String getLocalIpAddress() { 
    try { 
     for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { 
      NetworkInterface intf = en.nextElement(); 
      for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { 
       InetAddress inetAddress = enumIpAddr.nextElement(); 
       if (!inetAddress.isLoopbackAddress()) { 
        return inetAddress.getHostAddress().toString(); 
       } 
      } 
     } 
    } catch (SocketException ex) { 
     Log.e(LOG_TAG, ex.toString()); 
    } 
    return null; 
} 
+0

您也可以编辑return子句以返回所有值,而不仅仅是第一个。琐碎的工作,当然。没什么大不了。 – davidcesarino 2010-11-11 18:53:16

1

如果您为您的Android数据连接使用系绳应用,然后使用系绳连接到PC。找到ad hoc连接并连接。然后右键单击并选择状态。这会给你电话的IP地址。我使用Android应用程序“Barnicle WiFi tether”,并使用Windows 7 PC。