有沒有一種方法可以同時獲取Android和WiFi中的WiFi和蜂窩網絡的IP地址。我嘗試了很多示例,但都能夠獲得只有WiFi網絡的地址,而不是蜂窩網絡。我已經啓用了WiFi和蜂窩網絡,並且設備正在通過WiFi接入互聯網。如何在Android設備連接到WiFi時獲取蜂窩網絡的IP地址
這裏是我使用來獲取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.If蜂窩網絡的IP地址是如何是可行的。
我不認爲這是可能的。由於IP地址將被分配給您的IP通道,因此一次只能有一個通道處於活動狀態WIFI/MobileData。 –