AIM :Get IP address of device in android
Solution:
It returns NULL if there is no network connection.
public static String getipAddress() {
try {
for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
String ipaddress=inetAddress.getHostAddress().toString();
Log.e("ip address",""+ipaddress);
return ipaddress;
}
}
}
} catch (SocketException ex) {
Log.e("Socket exception in GetIP Address of Utilities", ex.toString());
}
return null;
}
try {
for (Enumeration
NetworkInterface intf = en.nextElement();
for (Enumeration
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
String ipaddress=inetAddress.getHostAddress().toString();
Log.e("ip address",""+ipaddress);
return ipaddress;
}
}
}
} catch (SocketException ex) {
Log.e("Socket exception in GetIP Address of Utilities", ex.toString());
}
return null;
}
Call the above method wherever you wnat to get ip address as
String ipaddress=getipAddress();
--------------------------