我的家園

          我的家園

          Android中判斷網絡連接狀態

          Posted on 2012-04-15 16:37 zljpp 閱讀(438) 評論(0)  編輯  收藏

          最近做項目,用到判斷網絡連接狀態,于是想寫個能兼容多種網絡模式的判斷工具,如下直接上代碼


          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 "";   
               
             }   
          }


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 桂东县| 永登县| 普洱| 苏州市| 油尖旺区| 康定县| 汽车| 尚志市| 象州县| 安塞县| 祁门县| 元阳县| 图木舒克市| 美姑县| 云龙县| 永州市| 彭泽县| 靖安县| 乌审旗| 沙田区| 平邑县| 庆云县| 辉南县| 福海县| 昌乐县| 黎城县| 洛扎县| 石城县| 永靖县| 南阳市| 吴忠市| 涪陵区| 大洼县| 蓝山县| 勐海县| 洛南县| 正蓝旗| 辽宁省| 大冶市| 武隆县| 揭阳市|