Is there any Simple Way to get the IP address Of my phone when connected to internet through mobile data Network. For getting WiFi IP address i am using following simple Technique.
WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
Is there any way similar to above to get IP address of mobile data network.
I have used following code but it returns MAC addresses ,IP addresses of both WiFi and cellular network but i am interested only in Cellular IP Address.
String ipAddress = null;
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()) {
ipAddress = inetAddress.getHostAddress().toString();
Log.i("Sarao5",ipAddress);
}
}
}
} catch (SocketException ex) {}
Should avoid using
Formatter.formatIPAddress
, use the following for getting the IP Address, minor differences from your code; its one function that returns wifi ip if its enabled else the cellular one that you require, you can modify it acc to need;Use below code as i use in my app -
this is best and easy way.
Hope my answer is helpfull.