通过3G网络,而无需使用无线网络发送的HTTP请求? [英] Send HTTP request through 3G network without using WiFi?

查看:166
本文介绍了通过3G网络,而无需使用无线网络发送的HTTP请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,Android手机同时连接3G网络和wifi。 在这个时候,我想通过3G网络,而无需使用无线网络发送的HTTP请求。 我该怎么办呢?

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?

推荐答案

我不认为你可以,因为在Android中这样做只有一个网络是活动的,在任何时间点。在这样的情况,首先您需要检查哪些网络是活动的,那么,如果它是一个无线网络之一,然后断开,那么Android将回退到另一种,这将是3G(如果没有其他的Wi-Fi网络提供) ,那么您可以把您的要求,将通过3G network.outline可能是这样的:

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
}

你可能会在所有激活N / W需要循环并断开它们,直到你找到的3G网络。我假设只有一个Wi-Fi网络和一个3G网络可用。

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.

这篇关于通过3G网络,而无需使用无线网络发送的HTTP请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆