当前位置: 移动技术网 > 移动技术>移动开发>Android > Android项目实战(五十三):判断网络连接是否为有线状态(tv项目适配)

Android项目实战(五十三):判断网络连接是否为有线状态(tv项目适配)

2018年12月25日  | 移动技术网移动技术  | 我要评论

一般对于android手机,我们可以通过sdk提供的方法判断网络情况

 

     /**
     * 获取当前的网络状态 :没有网络-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一个外网。

  

/* 
 * @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的区分

 

  

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网