博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android中判断网络连接状态
阅读量:5258 次
发布时间:2019-06-14

本文共 4276 字,大约阅读时间需要 14 分钟。

最近做项目,用到判断网络连接状态,于是想写个能兼容多种网络模式的判断工具,如下直接上代码

import java.util.Hashtable;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.DetailedState;
import android.util.Log;
import com.rk.jcz.util.FeedBackException;
/**
 * 
 * @author 刘伟
 * @date 2012-04-02
 * @QQ 574613441
 */
public class NetConnectManager {
private static final String LOG_TAG = "NetConnectManager" ;
/**  
     * 中国移动cmwap  
     */  
    public static String CMWAP = "cmwap";   
       
    /**  
     * 中国移动cmnet  
     */  
    public static String CMNET = "cmnet";   
       
    //中国联通3GWAP设置        中国联通3G因特网设置        中国联通WAP设置        中国联通因特网设置   
    //3gwap                 3gnet                uniwap            uninet   
       
       
    /**  
     * 3G wap 中国联通3gwap APN   
     */  
    public static String GWAP_3 = "3gwap";   
       
    /**  
     * 3G net 中国联通3gnet APN   
     */  
    public static String GNET_3="3gnet";   
       
    /**  
     * uni wap 中国联通uni wap APN   
     */  
    public static String UNIWAP="uniwap";   
    /**  
     * uni net 中国联通uni net APN   
     */  
    public static String UNINET="uninet";   
    
/**  
* 电信APN列表  
* @author wudongdong  
*  
*/  
   public static final String CTWAP="ctwap";   
   public static final String CTNET="ctnet";   
private static Hashtable<String,String> networkTable = new Hashtable<String,String>();
public static void switchNetType(Context context,String from,String to){
}
public static boolean isNetWorkAvailable(Context context) {
boolean isAvailable = false ;
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if(connectivityManager!=null){
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if(networkInfo!=null){
DetailedState detailedState = networkInfo.getDetailedState();
String detailedName = detailedState.name();
if(detailedName==null){
detailedName = "";
}
networkTable.put("detailedName", detailedName);
String extraInfo = networkInfo.getExtraInfo();
if(extraInfo==null){
extraInfo = "";
}
networkTable.put("extraInfo", extraInfo);
String reason = networkInfo.getReason();
if(reason==null){
reason = "";
}
networkTable.put("reason", reason);
NetworkInfo.State state = networkInfo.getState();
String stateName = "";
if(state!=null && state.name()!=null){
stateName=state.name();

}

// 经过多次测试,只有stateName可以准确的判断网络连接是否正常

if("CONNECTED".equalsIgnoreCase(stateName)){

isAvailable = true ;
}
networkTable.put("stateName", stateName);
int subType = networkInfo.getSubtype();
networkTable.put("subType", subType+"");
String subtypeName = networkInfo.getSubtypeName();
if(subtypeName==null){
subtypeName = "";
}
networkTable.put("subtypeName", subtypeName);
int type = networkInfo.getType();
String typeName = networkInfo.getTypeName();
if(typeName==null){
typeName = "";
}
networkTable.put("typeName", typeName);
Log.d(LOG_TAG,getLogString());
}
}
return isAvailable;
}
public static String getLogString(){
StringBuilder sb = new StringBuilder();
sb.append("detailedName="+networkTable.get("detailedName"));
sb.append(" extraInfo="+networkTable.get("extraInfo"));
sb.append(" reason="+networkTable.get("reason"));
sb.append(" stateName="+networkTable.get("stateName"));
sb.append(" subtypeName="+networkTable.get("subtypeName"));
sb.append(" typeName="+networkTable.get("typeName"));
return sb.toString();
}
public static String apnType(){
String apn = "" ;
String netType = networkTable.get("typeName");
if("CONNECTED".equals(networkTable.get("stateName"))){
if("wifi".equalsIgnoreCase(netType)){
apn = netType;
}else if("mobile".equalsIgnoreCase(netType)){
netType = networkTable.get("extraInfo").toLowerCase();    
   apn = matchAPN(netType);
}else{
new FeedBackException("UNKNOW networkType:::"+getLogString());
}
}else{
}
return apn ;
}
public static String matchAPN(String currentName) {           
       if("".equals(currentName) || null==currentName){   
           return "";   
       }   
       currentName = currentName.toLowerCase();   
       if(currentName.startsWith(CMNET))   
           return CMNET;   
       else if(currentName.startsWith(CMWAP))   
           return CMWAP;   
       else if(currentName.startsWith(GNET_3))   
           return GNET_3;   
       else if(currentName.startsWith(GWAP_3))   
           return GWAP_3;   
       else if(currentName.startsWith(UNINET))   
           return UNINET;   
       else if(currentName.startsWith(UNIWAP))   
           return UNIWAP;
       else if(currentName.startsWith(CTWAP))   
           return CTWAP;
       else if(currentName.startsWith(CTNET))   
           return CTNET;
       else if(currentName.startsWith("default"))   
           return "default";   
       else return "";   
     
   }   
}

转载于:https://www.cnblogs.com/mixer/archive/2012/04/12/2444476.html

你可能感兴趣的文章
题解: [GXOI/GZOI2019]与或和
查看>>
MacOS copy图标shell脚本
查看>>
国外常见互联网盈利创新模式
查看>>
Oracle-05
查看>>
linux grep 搜索查找
查看>>
Not enough free disk space on disk '/boot'(转载)
查看>>
android 签名
查看>>
vue项目中使用百度统计
查看>>
android:scaleType属性
查看>>
SuperEPC
查看>>
mysql-5.7 innodb 的并行任务调度详解
查看>>
shell脚本
查看>>
Upload Image to .NET Core 2.1 API
查看>>
Js时间处理
查看>>
Java项目xml相关配置
查看>>
三维变换概述
查看>>
第三次作业
查看>>
vue route 跳转
查看>>
【雷电】源代码分析(二)-- 进入游戏攻击
查看>>
Entityframework:“System.Data.Entity.Internal.AppConfig”的类型初始值设定项引发异常。...
查看>>