Android項目實戰(五十三):判斷網絡鏈接是否爲有線狀態(tv項目適配)

通常對於android手機,咱們能夠經過sdk提供的方法判斷網絡狀況android

 

     /**
     * 獲取當前的網絡狀態 :沒有網絡-0:WIFI網絡1:4G網絡-4:3G網絡-3:2G網絡-2
     * 自定義
     *
     * @param context
     * @return
     */
    public static int getAPNType(Context context) {
        //結果返回值
        int netType = 0;
        //獲取手機全部鏈接管理對象
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        //獲取NetworkInfo對象
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        //NetworkInfo對象爲空 則表明沒有網絡
        if (networkInfo == null) {
            return netType;
        }
        //不然 NetworkInfo對象不爲空 則獲取該networkInfo的類型
        int nType = networkInfo.getType();
        if (nType == ConnectivityManager.TYPE_WIFI) {
            //WIFI
            netType = 1;
        } else if (nType == ConnectivityManager.TYPE_MOBILE) {
            int nSubType = networkInfo.getSubtype();
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            //3G   聯通的3G爲UMTS或HSDPA 電信的3G爲EVDO
            if (nSubType == TelephonyManager.NETWORK_TYPE_LTE
                    && !telephonyManager.isNetworkRoaming()) {
                netType = 4;
            } else if (nSubType == TelephonyManager.NETWORK_TYPE_UMTS
                    || nSubType == TelephonyManager.NETWORK_TYPE_HSDPA
                    || nSubType == TelephonyManager.NETWORK_TYPE_EVDO_0
                    && !telephonyManager.isNetworkRoaming()) {
                netType = 3;
                //2G 移動和聯通的2G爲GPRS或EGDE,電信的2G爲CDMA
            } else if (nSubType == TelephonyManager.NETWORK_TYPE_GPRS
                    || nSubType == TelephonyManager.NETWORK_TYPE_EDGE
                    || nSubType == TelephonyManager.NETWORK_TYPE_CDMA
                    && !telephonyManager.isNetworkRoaming()) {
                netType = 2;
            } else {
                netType = 2;
            }
        }
        return netType;
    }

 

  注意的是對於Tv項目,android系統的Tv(好比小米電視),有的是支持有線鏈接的(非wifi,2g 3g 4g)的 , 此時上述方法會判斷爲0,無網絡鏈接狀態,因此對於Tv項目,須要對網絡適配進行兼容網絡

  解決辦法就是ping一個外網。app

  

/* 
 * @category 判斷是否有外網鏈接(普通方法不能判斷外網的網絡是否鏈接,好比鏈接上局域網)
 * @return
 */ 
public static final boolean ping() { 

  String result = null; 
  try { 
      String ip = "www.baidu.com";// ping 的地址,能夠換成任何一種可靠的外網 
      Process p = Runtime.getRuntime().exec("ping -c 3 -w 100 " + ip);// ping網址3次 
      // 讀取ping的內容,能夠不加 
      InputStream input = p.getInputStream(); 
      BufferedReader in = new BufferedReader(new InputStreamReader(input)); 
      StringBuffer stringBuffer = new StringBuffer(); 
      String content = ""; 
      while ((content = in.readLine()) != null) { 
          stringBuffer.append(content); 
      } 
      Log.d("------ping-----", "result content : " + stringBuffer.toString()); 
      // ping的狀態 
      int status = p.waitFor(); 
      if (status == 0) { 
          result = "success"; 
          return true; 
      } else { 
          result = "failed"; 
      } 
  } catch (IOException e) { 
      result = "IOException"; 
  } catch (InterruptedException e) { 
      result = "InterruptedException"; 
  } finally { 
      Log.d("----result---", "result = " + result); 
  } 
  return false;
}

 

  由此能夠對網絡狀態進行: 有線/wifi/2g/3g/4g的區分spa

相關文章
相關標籤/搜索