2012-02-24 53 views
0

我使用下面的代码通过Android设备上的网络接口迭代:确实有没有wifi连接时,android会放弃蜂窝连接?

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,然后我打开无线网络,突然它只能恢复无线IP)。

所以有谁知道会发生什么?如果我在iPhone上使用类似的代码,我会同时获得蜂窝和无线IP。

+2

作为Android手机用户,我不得不说,是的,Android会在WiFi连接时丢弃移动数据连接(并且数据连接图标会消失)。这应该是减少系统移动数据使用的保护措施。 – coolcfan 2012-02-24 09:45:57

+0

这是正确的答案。当您启动Android电子市场时,您会注意到它会短暂打开,以便市场可以检查您的网络标题以确定您所在的国家/地区。 – 2012-02-24 10:01:09

回答

1

是的,android一次只保留一个wifi或蜂窝连接。它看起来像ConnectivityManager.requestRouteToHost将允许你强制通过特定的传输连接,但我个人从未尝试过。