有没有一种方法可以同时获取Android中WiFi和蜂窝网络的IP地址。我尝试使用许多示例,但只能获取WiFi网络的地址而不是蜂窝网络的地址。我已经启用了WiFi和蜂窝网络,并且设备通过WiFi具有Internet访问权限。
这是我正在使用的获取IP地址的代码:
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("Here is the Address",ipAddress);
}
}
}
} catch (SocketException ex) {
}
当设备连接到WiFi时,是否能够获取蜂窝网络的IP地址。如果可以,那么如何实现呢?