我使用以下代码来迭代 android 上的网络接口(interface):
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()) {
Log.e("get ip: ", inetAddress.getHostAddress());
}
}
}
但是如果我连接到wifi网络,我只会获得wifi IP,我期望同时获得wifi和蜂窝IP(手机在蜂窝网络上并获得IP,然后我打开wifi,然后突然它只返回 wifi ip)。
有人知道会发生什么吗?如果我在 iPhone 上使用类似的代码,我会同时获得蜂窝和 WiFi IP。
最佳答案
是的,Android 一次仅保持 wifi 或蜂窝网络之一的连接。看起来 ConnectivityManager.requestRouteToHost 将允许您通过特定传输强制连接,但我个人从未尝试过。
关于java - 当有 wifi 连接时,android 会断开蜂窝连接吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9428495/