android 网络判断
来源:互联网 发布:天津好吃的地方知乎 编辑:程序博客网 时间:2024/06/04 00:53
转自
package mark.zhang;import java.util.List;import android.content.Context;import android.location.LocationManager;import android.net.ConnectivityManager;import android.net.NetworkInfo;import android.telephony.TelephonyManager;public class NetworkProber {/** * 网络是否可用 * * @param activity * @return */public static boolean isNetworkAvailable(Context context) {ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);if (connectivity == null) {} else {NetworkInfo[] info = connectivity.getAllNetworkInfo();if (info != null) {for (int i = 0; i < info.length; i++) {if (info[i].getState() == NetworkInfo.State.CONNECTED) {return true;}}}}return false;}/** * Gps是否打开 * * @param context * @return */public static boolean isGpsEnabled(Context context) {LocationManager locationManager = ((LocationManager) context.getSystemService(Context.LOCATION_SERVICE));List<String> accessibleProviders = locationManager.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);}/** * 判断当前网络是否是wifi网络 * if(activeNetInfo.getType()==ConnectivityManager.TYPE_MOBILE) { //判断3G网 * * @param context * @return boolean */public static boolean isWifi(Context context) {ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();if (activeNetInfo != null&& activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {return true;}return false;}/** * 判断当前网络是否是3G网络 * * @param context * @return boolean */public static boolean is3G(Context context) {ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();if (activeNetInfo != null&& activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE) {return true;}return false;}}
另外还有两个方法判断网络是否可用:
public static boolean isNetworkAvailable_00(Context context) { ConnectivityManager cm = ((ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE)); if (cm != null) { NetworkInfo info = cm.getActiveNetworkInfo(); if (info != null && info.isConnectedOrConnecting()) { return true; } } return false; } public static boolean isNetworkAvailable_01(Context context) { ConnectivityManager cm = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo network = cm.getActiveNetworkInfo(); if (network != null) { return network.isAvailable(); } return false; }
更加严谨的写法:
public static boolean checkNet(Context context) { try { ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity != null) { NetworkInfo info = connectivity.getActiveNetworkInfo(); if (info != null && info.isConnected()) { if (info.getState() == NetworkInfo.State.CONNECTED) { return true; } } } } catch (Exception e) { return false; } return false; }
- Android网络判断,网络设置
- android 判断网络连接
- Android中判断网络
- network: android 网络判断
- android 判断网络状态
- android 判断网络连接
- android 网络判断
- android 网络判断
- network: android 网络判断
- android 判断网络连接
- network: android 网络判断
- Android判断网络状态
- Android 判断网络状态
- android 网络状态判断
- android之网络判断
- network: android 网络判断
- network: android 网络判断
- android 判断网络连接
- 自定义TabHost,TabWidget样式 .
- Some characters cannot be mapped using "Cp1252" character encoding 解决方法
- 进程隐藏与进程保护(SSDT Hook 实现)(一)
- layout_gravity和gravity 关于属性失效问题
- 进程隐藏与进程保护(SSDT Hook 实现)(二)
- android 网络判断
- 闲来无事,复习复习以前的东西---hibernate中的关联映射图解 ---- 复合主键
- Nginx下Magento伪静态规则,适用于LNMP一键包
- C语言初学者的十八颗“地雷”
- 详解Android中AsyncTask的使用
- happens-before原则
- RSA加密解密-实例
- hdu2818
- tomcat配置Gzip压缩功能