Android:如何从Android应用程序中获取IP地址?

4

从 Android 应用程序中获取 IP 地址是否可行?


请查看此博客文章:http://www.droidnova.com/get-the-ip-address-of-your-device,304.html - Sean W.
2个回答

3

我曾经收藏了这个链接,但一直没有测试过:

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

该链接涉及IT技术,介绍如何获取设备的IP地址。
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;
}

当然,这是微不足道的工作,没有什么大不了的。您还可以编辑返回子句,以返回所有值而不仅仅是第一个值。 - davidcesarino

1
如果您使用 Android 数据连接的连接共享应用程序,则可以使用该应用程序来将 Android 设备连接到电脑。找到点对点连接并进行连接,然后右键单击并选择状态。这将提供您手机的 IP 地址。我使用的是 Android 应用程序“Barnicle WiFi tether”并且正在使用 Windows 7 电脑。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接