有没有办法我可以在
Android中同时获取WiFi和蜂窝网络的IP地址.我尝试使用很多例子,但是只能获得WiFi网络的地址而不是蜂窝网络.我启用了WiFi和蜂窝网络,设备正在通过WiFi上网.
以下是我用来获取IP地址的代码:
String ipAddress = null;
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()) {
ipAddress = inetAddress.getHostAddress().toString();
Log.i("Here is the Address",ipAddress);
}
}
}
} catch (SocketException ex) {
}
当设备连接到WiFi时,是否可以获取蜂窝网络的IP地址.如果是可行的话,是可以的.