簡體   English   中英

Android:如何從Android應用程序獲取IP地址?

[英]Android: How can I get ip address from an android app?

是否有可能從Android應用程序獲取IP地址?

我將它保留在我的書簽中一段時間​​了,但從未測試過它:

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;
}

如果您使用系繩應用程序進行Android數據連接,請使用系繩連接到PC。 找到ad hoc連接並連接。 然后右鍵單擊並選擇狀態。 這將為您提供手機的IP地址。 我使用Android應用程序“Barnicle WiFi tether”並使用Windows 7 PC。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM