2012-02-24 51 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將允許你強制通過特定的傳輸連接,但我個人從未嘗試過。