当前位置: 移动技术网 > IT编程>移动开发>Android > Android中判断网络连接是否可用的方法总结

Android中判断网络连接是否可用的方法总结

2019年07月24日  | 移动技术网IT编程  | 我要评论

本宫不好惹,疯狂鉴黄师答案,冀星高速

android 网路判断

判断当前网络是否是wifi

/**
   * 判断当前是否是wifi
   * @param mcontext
   * @return
   */
  private static boolean iswifi(context mcontext) { 
    connectivitymanager connectivitymanager = (connectivitymanager) mcontext 
        .getsystemservice(context.connectivity_service); 
    networkinfo activenetinfo = connectivitymanager.getactivenetworkinfo(); 
    if (activenetinfo != null 
        && activenetinfo.gettype() == connectivitymanager.type_wifi) { 
      return true; 
    } 
    return false; 
  } 
}

一、判断网络连接是否可用

public static boolean isnetworkavailable(context context) { 
 connectivitymanager cm = (connectivitymanager) context 
  .getsystemservice(context.connectivity_service); 
 if (cm == null) { 
 } else {
       //如果仅仅是用来判断网络连接
       //则可以使用 cm.getactivenetworkinfo().isavailable(); 
  networkinfo[] info = cm.getallnetworkinfo(); 
  if (info != null) { 
  for (int i = 0; i < info.length; i++) { 
   if (info[i].getstate() == networkinfo.state.connected) { 
   return true; 
   } 
  } 
  } 
 } 
 return false; 
 } 

  二、判断gps是否打开

 public static boolean isgpsenabled(context context) { 
 locationmanager lm = ((locationmanager) context 
  .getsystemservice(context.location_service)); 
 list<string> accessibleproviders = lm.getproviders(true); 
 return accessibleproviders != null && accessibleproviders.size() > 0; 
 } 

 三、判断wifi是否打开

public static boolean iswifienabled(context context) { 
 connectivitymanager mgrconn = (connectivitymanager) context 
  .getsystemservice(context.connectivity_service); 
 telephonymanager mgrtel = (telephonymanager) context 
  .getsystemservice(context.telephony_service); 
 return ((mgrconn.getactivenetworkinfo() != null && mgrconn 
  .getactivenetworkinfo().getstate() == networkinfo.state.connected) || mgrtel 
  .getnetworktype() == telephonymanager.network_type_umts); 
 } 

四、判断是否是3g网络

 public static boolean is3rd(context context) { 
 connectivitymanager cm = (connectivitymanager) context 
  .getsystemservice(context.connectivity_service); 
 networkinfo networkinfo = cm.getactivenetworkinfo(); 
 if (networkinfo != null 
  && networkinfo.gettype() == connectivitymanager.type_mobile) { 
  return true; 
 } 
 return false; 
 } 

五、判断是wifi还是3g网络,用户的体现性在这里了,wifi就可以建议下载或者在线播放。

public static boolean iswifi(context context) { 
  connectivitymanager cm = (connectivitymanager) context 
   .getsystemservice(context.connectivity_service); 
  networkinfo networkinfo = cm.getactivenetworkinfo(); 
  if (networkinfo != null 
   && networkinfo.gettype() == connectivitymanager.type_wifi) { 
  return true; 
  } 
  return false; 
 }

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网