2014-01-27 33 views
0

在我的一個應用程序中,我只需要在2G/3g/4g上發出HTTP請求。如果設備連接到Wifi和移動數據,我需要檢查並通過移動數據進行HTTP請求。我不想打開/關閉Wifi。對移動數據的Android HTTP請求

如果可以通過Socket編程完成,我們可以在兩個網絡都可用的情況下在蜂窩網絡上傳輸流量。

任何示例代碼或教程鏈接將不勝感激。

+1

該設備自動選擇最快的連接可用 – onkar

回答

1

嘗試實現這個Connectivity java類並檢查您的移動數據/ WiFi。

public class Connectivity { 


public static NetworkInfo getNetworkInfo(Context context){ 
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
    return cm.getActiveNetworkInfo(); 
} 


public static boolean isConnected(Context context){ 
    NetworkInfo info = Connectivity.getNetworkInfo(context); 
    return (info != null && info.isConnected()); 
} 


public static boolean isConnectedWifi(Context context){ 
    NetworkInfo info = Connectivity.getNetworkInfo(context); 
    return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI); 
} 


public static boolean isConnectedMobile(Context context){ 
    NetworkInfo info = Connectivity.getNetworkInfo(context); 
    return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_MOBILE); 
} 


public static boolean isConnectedFast(Context context){ 
    NetworkInfo info = Connectivity.getNetworkInfo(context); 
    return (info != null && info.isConnected() && Connectivity.isConnectionFast(info.getType(),info.getSubtype())); 
} 


public static boolean isConnectionFast(int type, int subType){ 
    if(type==ConnectivityManager.TYPE_WIFI){ 
     return true; 
    }else if(type==ConnectivityManager.TYPE_MOBILE){ 
     switch(subType){ 
     case TelephonyManager.NETWORK_TYPE_1xRTT: 
      return false; // ~ 50-100 kbps 
     case TelephonyManager.NETWORK_TYPE_CDMA: 
      return false; // ~ 14-64 kbps 
     case TelephonyManager.NETWORK_TYPE_EDGE: 
      return false; // ~ 50-100 kbps 
     case TelephonyManager.NETWORK_TYPE_EVDO_0: 
      return true; // ~ 400-1000 kbps 
     case TelephonyManager.NETWORK_TYPE_EVDO_A: 
      return true; // ~ 600-1400 kbps 
     case TelephonyManager.NETWORK_TYPE_GPRS: 
      return false; // ~ 100 kbps 
     case TelephonyManager.NETWORK_TYPE_HSDPA: 
      return true; // ~ 2-14 Mbps 
     case TelephonyManager.NETWORK_TYPE_HSPA: 
      return true; // ~ 700-1700 kbps 
     case TelephonyManager.NETWORK_TYPE_HSUPA: 
      return true; // ~ 1-23 Mbps 
     case TelephonyManager.NETWORK_TYPE_UMTS: 
      return true; // ~ 400-7000 kbps 
     /* 
     * Above API level 7, make sure to set android:targetSdkVersion 
     * to appropriate level to use these 
     */ 
     case TelephonyManager.NETWORK_TYPE_EHRPD: // API level 11 
      return true; // ~ 1-2 Mbps 
     case TelephonyManager.NETWORK_TYPE_EVDO_B: // API level 9 
      return true; // ~ 5 Mbps 
     case TelephonyManager.NETWORK_TYPE_HSPAP: // API level 13 
      return true; // ~ 10-20 Mbps 
     case TelephonyManager.NETWORK_TYPE_IDEN: // API level 8 
      return false; // ~25 kbps 
     case TelephonyManager.NETWORK_TYPE_LTE: // API level 11 
      return true; // ~ 10+ Mbps 
     // Unknown 
     case TelephonyManager.NETWORK_TYPE_UNKNOWN: 
     default: 
      return false; 
     } 
    }else{ 
     return false; 
    } 
} 

} 

,並檢查象下面這樣:

if(Connectivity.isConnectedWifi(Login.this)){ 
       //Implement your logic  

}else if(Connectivity.isConnectedMobile(Login.this)){ 
       //Implement your logic    
} 

更新:

嘗試實現第二種方式:

我不認爲你可以因爲在Android中做到這一點只一個網絡在任何時間點都處於活動狀態。因此,首先需要檢查哪個網絡處於活動狀態,如果是Wi-Fi網絡,則將其斷開連接,然後Android將回退到其他網絡,這將是2G/3G(如果沒有其他Wi-Fi網絡可用),那麼你可以把您的要求,將經過2G/3G network.outline可能是這樣的:

ConnectivityManager cm = (ConnectivityManager)Context.getSystemService(Context.CONNECTIVITY_SERVICE); 
NetworkInfo ni = cm.getActiveNetworkInfo(); 
if(ni == null) 
//no connectivity, abort 
if(ni.getType() == ConnectivityManager.TYPE_WIFI || ni.getType() == ConnectivityManager.TYPE_WIMAX) { 
WifiManager wm = (WifiManager)Context.getSystemService(Context.WIFI_SERVICE); 
if(wm != null) 
wm.disconnect(); 
//this will force android to fallback to other available n/w which is 3G 
} 
while(true) { 
NetworkInfo ni = cm.getActiveNetworkInfo(); 
if(ni != null && ni.getType() == ConnectivityManager.TYPE_MOBILE && ni.isConnected()) { 
    //send your http request 
break; 

} // 睡了一段時間,讓機器人可以連接到其他N/w }

您可能需要遍歷所有活動的n/w並斷開它們直到找到2G/3G網絡。我假設只有一個Wi-Fi網絡和一個2G/3G網絡。

在這裏您可以根據您的要求進行定製,也可以使用我的Connectivity.java課程,並通過整合。

+0

通過這些方法,我只能檢查,但我想在2G/3G上發出HTTP請求,如果兩個連接都可用。我如何做到這一點 –

+0

你爲什麼不想使用WiFi?我認爲如果你的用戶只有在你的應用浪費了他們的數據的情況下打開了wifi才能讓你的用戶感到非常高興。也許更好的解決方案是提示用戶關閉他們的無線網絡,如果它打開,所以用戶知道發生了什麼。 – NasaGeek

+0

@Android_Code_Chef查看我的更新 –

0

使用下面的代碼

public class ConnectionDetector { 

private Context _context; 

public ConnectionDetector(Context context) { 
    this._context = context; 
} 


public boolean isConnectingToInternet() { 
    boolean haveConnectedWifi = false; 
    boolean haveConnectedMobile = false; 

    ConnectivityManager cm = (ConnectivityManager) _context 
      .getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo[] netInfo = cm.getAllNetworkInfo(); 
    for (NetworkInfo ni : netInfo) { 
     if (ni.getTypeName().equalsIgnoreCase("WIFI")) 
      if (ni.isConnected()) 
       haveConnectedWifi = true; 
     if (ni.getTypeName().equalsIgnoreCase("MOBILE")) 
      if (ni.isConnected()) 
       haveConnectedMobile = true; 
    } 
    return haveConnectedMobile; 
} 

} 

它只會在移動網絡連接互聯網。