2010-11-11 41 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。