欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android检查手机网络状态及网络类型的方法

程序员文章站 2022-06-23 10:17:15
本文实例讲述了android检查手机网络状态及网络类型的方法。分享给大家供大家参考。具体分析如下: //judge network status is conne...

本文实例讲述了android检查手机网络状态及网络类型的方法。分享给大家供大家参考。具体分析如下:

//judge network status is connecting or not
 public static boolean checknetworkconnected(context context) { 
 if (null!=context) { 
 connectivitymanager connectivitymanager = (connectivitymanager) context
 .getsystemservice(context.connectivity_service); 
 networkinfo networkinfo = connectivitymanager.getactivenetworkinfo();
 if (null!=networkinfo) { 
  return networkinfo.isavailable(); 
  } 
 } 
  return false; 
 } 
 //judge wifi status is connecting or not
 public static boolean checkwifistatus(context context) { 
 connectivitymanager connectivitymanager = (connectivitymanager) context 
 .getsystemservice(context.connectivity_service); 
 networkinfo networkinfo = connectivitymanager.getactivenetworkinfo(); 
 if (null!=networkinfo 
 && networkinfo.gettype() == connectivitymanager.type_wifi) { 
 return true; 
  } 
 return false; 
 } 
 //judge 3g status is connecting or not
 public static boolean check3gstatus(context context) { 
 connectivitymanager connectivitymanager = (connectivitymanager) context 
 .getsystemservice(context.connectivity_service); 
 networkinfo networkinfo = connectivitymanager.getactivenetworkinfo(); 
 if (null!=networkinfo 
 && networkinfo.gettype() == connectivitymanager.type_mobile) { 
 return true; 
  }
 return false; 
 }

希望本文所述对大家的android程序设计有所帮助。