tags:

views:

83

answers:

1

First of all, Android phone is connected both 3G network and wifi. At this time I'd like to send http request through 3G network without using wifi. How can I do it?

Possible Duplicate: http://stackoverflow.com/questions/3605643/id-like-to-send-http-request-through-3g-network-when-wifi-is-connected-in-androi

+1  A: 

I don't think you can do this because in Android only one Network is active at any point of time. So for that first you need to check which network is active and then if it is a Wi-Fi one, then disconnect it, then Android will fallback to other one which will be 3G (if there is no other wi-fi network available), then you can send your request which will go through 3G network.outline might look like this:

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;
  }
  //sleep for some time, so that android can connect you to other n/w
}
You might need to loop through all active n/w and disconnect them till you find 3G network. I am assuming that there is just one Wi-Fi network and one 3G network available.

bhups