Android 如何使用3G连接代替Wi-fi?
如何在Android应用程序中使用3G连接而不是Wi-fi?Android 如何使用3G连接代替Wi-fi?,android,Android,如何在Android应用程序中使用3G连接而不是Wi-fi? 我想连接3G连接,有没有连接到3G而不是Wi-fi的示例代码?只需从另一篇文章复制和粘贴即可 /** * Enable mobile connection for a specific address * @param context a Context (application or activity) * @param address the address to enable * @return true for suc
我想连接3G连接,有没有连接到3G而不是Wi-fi的示例代码?只需从另一篇文章复制和粘贴即可
/**
* Enable mobile connection for a specific address
* @param context a Context (application or activity)
* @param address the address to enable
* @return true for success, else false
*/
private boolean forceMobileConnectionForAddress(Context context, String address) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (null == connectivityManager) {
Log.debug(TAG_LOG, "ConnectivityManager is null, cannot try to force a mobile connection");
return false;
}
//check if mobile connection is available and connected
State state = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_HIPRI).getState();
Log.debug(TAG_LOG, "TYPE_MOBILE_HIPRI network state: " + state);
if (0 == state.compareTo(State.CONNECTED) || 0 == state.compareTo(State.CONNECTING)) {
return true;
}
//activate mobile connection in addition to other connection already activated
int resultInt = connectivityManager.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE, "enableHIPRI");
Log.debug(TAG_LOG, "startUsingNetworkFeature for enableHIPRI result: " + resultInt);
//-1 means errors
// 0 means already enabled
// 1 means enabled
// other values can be returned, because this method is vendor specific
if (-1 == resultInt) {
Log.error(TAG_LOG, "Wrong result of startUsingNetworkFeature, maybe problems");
return false;
}
if (0 == resultInt) {
Log.debug(TAG_LOG, "No need to perform additional network settings");
return true;
}
//find the host name to route
String hostName = StringUtil.extractAddressFromUrl(address);
Log.debug(TAG_LOG, "Source address: " + address);
Log.debug(TAG_LOG, "Destination host address to route: " + hostName);
if (TextUtils.isEmpty(hostName)) hostName = address;
//create a route for the specified address
int hostAddress = lookupHost(hostName);
if (-1 == hostAddress) {
Log.error(TAG_LOG, "Wrong host address transformation, result was -1");
return false;
}
//wait some time needed to connection manager for waking up
try {
for (int counter=0; counter<30; counter++) {
State checkState = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_HIPRI).getState();
if (0 == checkState.compareTo(State.CONNECTED))
break;
Thread.sleep(1000);
}
} catch (InterruptedException e) {
//nothing to do
}
boolean resultBool = connectivityManager.requestRouteToHost(ConnectivityManager.TYPE_MOBILE_HIPRI, hostAddress);
Log.debug(TAG_LOG, "requestRouteToHost result: " + resultBool);
if (!resultBool)
Log.error(TAG_LOG, "Wrong requestRouteToHost result: expected true, but was false");
return resultBool;
}
/**
*为特定地址启用移动连接
*@param context上下文(应用程序或活动)
*@param address要启用的地址
*@return true表示成功,否则为false
*/
专用布尔forceMobileConnectionForAddress(上下文、字符串地址){
ConnectivityManager ConnectivityManager=(ConnectivityManager)context.getSystemService(context.CONNECTIVITY_服务);
if(null==connectivityManager){
debug(TAG_Log,“ConnectionManager为空,无法尝试强制移动连接”);
返回false;
}
//检查移动连接是否可用并已连接
State State=connectivityManager.getNetworkInfo(connectivityManager.TYPE_MOBILE_HIPRI.getState();
调试(TAG_Log,“TYPE_MOBILE_HIPRI network state:“+state”);
如果(0==state.compareTo(state.CONNECTED)| | 0==state.compareTo(state.CONNECTING)){
返回true;
}
//除已激活的其他连接外,还激活移动连接
int resultInt=connectivityManager.startUsingNetworkFeature(connectivityManager.TYPE_MOBILE,“enableHIPRI”);
调试(TAG_Log,“enableHIPRI结果的startUsingNetworkFeature:”+resultInt);
//-1表示错误
//0表示已启用
//1表示启用
//可以返回其他值,因为此方法是特定于供应商的
如果(-1==resultInt){
Log.error(TAG_Log,“StartSingNetworkFeature的错误结果,可能是问题”);
返回false;
}
if(0==resultInt){
调试(TAG_Log,“无需执行其他网络设置”);
返回true;
}
//查找要路由的主机名
String hostName=StringUtil.extractAddressFromUrl(地址);
调试(标记日志,“源地址:”+地址);
调试(TAG_Log,“路由的目标主机地址:”+主机名);
如果(TextUtils.isEmpty(hostName))hostName=address;
//为指定的地址创建路由
int hostAddress=lookupHost(主机名);
如果(-1==主机地址){
错误(TAG_Log,“错误的主机地址转换,结果为-1”);
返回false;
}
//等待连接管理器唤醒所需的时间
试一试{
用于(整数计数器=0;计数器0){
urlToProcess=url.substring(protocolendix+3);
}否则{
urlToProcess=url;
}
//如果我们在地址中有端口号,我们将删除所有内容
//在端口号之后
int pos=urlToProcess.indexOf(“:”);
如果(位置>=0){
urlToProcess=urlToProcess.substring(0,pos);
}
//如果我们在地址中有资源位置,那么我们将
//“/”之后的一切
pos=urlToProcess.indexOf('/');
如果(位置>=0){
urlToProcess=urlToProcess.substring(0,pos);
}
//如果我们有?在地址里然后我们脱光衣服
//“?”之后的一切
pos=urlToProcess.indexOf('?');
如果(位置>=0){
urlToProcess=urlToProcess.substring(0,pos);
}
返回urlToProcess;
}
/**
*在{@link ConnectivityManager.requestRouteToHost}使用的int值中转换主机名
*方法
*
*@param主机名
*@return-1如果主机不存在,则在其他位置显示其翻译
*到整数
*/
专用静态int lookupHost(字符串主机名){
InetAddress InetAddress;
试一试{
inetAddress=inetAddress.getByName(主机名);
}捕获(未知后异常e){
返回-1;
}
字节[]加上字节;
国际地址;
addrBytes=inetAddress.getAddress();
addr=((addrBytes[3]&0xff)谢谢…很抱歉重复
/**
* This method extracts from address the hostname
* @param url eg. http://some.where.com:8080/sync
* @return some.where.com
*/
public static String extractAddressFromUrl(String url) {
String urlToProcess = null;
//find protocol
int protocolEndIndex = url.indexOf("://");
if(protocolEndIndex>0) {
urlToProcess = url.substring(protocolEndIndex + 3);
} else {
urlToProcess = url;
}
// If we have port number in the address we strip everything
// after the port number
int pos = urlToProcess.indexOf(':');
if (pos >= 0) {
urlToProcess = urlToProcess.substring(0, pos);
}
// If we have resource location in the address then we strip
// everything after the '/'
pos = urlToProcess.indexOf('/');
if (pos >= 0) {
urlToProcess = urlToProcess.substring(0, pos);
}
// If we have ? in the address then we strip
// everything after the '?'
pos = urlToProcess.indexOf('?');
if (pos >= 0) {
urlToProcess = urlToProcess.substring(0, pos);
}
return urlToProcess;
}
/**
* Transform host name in int value used by {@link ConnectivityManager.requestRouteToHost}
* method
*
* @param hostname
* @return -1 if the host doesn't exists, elsewhere its translation
* to an integer
*/
private static int lookupHost(String hostname) {
InetAddress inetAddress;
try {
inetAddress = InetAddress.getByName(hostname);
} catch (UnknownHostException e) {
return -1;
}
byte[] addrBytes;
int addr;
addrBytes = inetAddress.getAddress();
addr = ((addrBytes[3] & 0xff) << 24)
| ((addrBytes[2] & 0xff) << 16)
| ((addrBytes[1] & 0xff) << 8 )
| (addrBytes[0] & 0xff);
return addr;
}
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />