Android中判断网络连接是否可用的方法总结
程序员文章站
2022-06-06 17:12:44
android 网路判断
判断当前网络是否是wifi
/**
* 判断当前是否是wifi
* @param mcontext
* @re...
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; }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
上一篇: 字符串抽取转化成数组KEY值