开发者

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

开发者 https://www.devze.com 2023-01-24 09:29 出处:网络
Is it possible to get the ip address 开发者_Go百科from android app?I kept this in my bookmark for some time, but never got to test it:

Is it possible to get the ip address 开发者_Go百科from android app?


I kept this in my bookmark for some time, but never got to test it:

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


If you use a tether app for your Android data connection, then use the tether to connect to PC. Find the ad hoc connection and connect. Then right click and select status. This will give you the phone's IP address. I use the Android app "Barnicle WiFi tether" and am using a Windows 7 PC.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号