美文网首页
检测是否有可用网络及网络连接的类型

检测是否有可用网络及网络连接的类型

作者: GaoXiaoGao | 来源:发表于2019-05-17 11:28 被阅读0次
    public class NetStatusUtils {
     
        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;
        }
    
        /**
         * 判断当前网络是否是wifi网络
         */
        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;
        }
    
        /**
         * 判断当前网络是否移动网络
         *
         * @param context
         * @return boolean
         */
        public static boolean isMobileNetwork(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;
        }
    }
    

    相关文章

      网友评论

          本文标题:检测是否有可用网络及网络连接的类型

          本文链接:https://www.haomeiwen.com/subject/ndagaqtx.html