Android: How can I get ip address from an android app?
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.
精彩评论