判断手机网络类型

1、判断当前是否有网络 
2、判断当前网络类型 
3、判断当前手机网络类型 
4、判断当前手机网络2G\3G\4G

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

public class NetWorkManagementUtils {

         /**
         * 判断当前的网络是否可用
         * @param context
         * @return
         */
        public  boolean  isNetWorkUtils(Context context){

            //
            ConnectivityManager conManager=(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

            if(conManager!=null){
                NetworkInfo info = conManager.getActiveNetworkInfo();
                    if(info!=null){
                        return info.isAvailable();
                    }
            }
            return false;
        }

        /**
         * 判断当前是手机网络还是wifi
         * @param context
         * @return
         */

        public  String  isWifiUtils(Context context){

            ConnectivityManager conManager=(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

            NetworkInfo info = conManager.getActiveNetworkInfo();
            if(isNetWorkUtils(context)){
                if(info.getType() == ConnectivityManager.TYPE_WIFI){

                        return "is wifi";
                    }else if(info.getType() ==  ConnectivityManager.TYPE_MOBILE){

                        return "is mobile net";
                    }else{
                        return "other";
                    }
            }
            return "no net";
        }

            /** Network type is unknown */
            public static final int NETWORK_TYPE_UNKNOWN = 0;
            /** Current network is GPRS */
            public static final int NETWORK_TYPE_GPRS = 1;
            /** Current network is EDGE */
            public static final int NETWORK_TYPE_EDGE = 2;
            /** Current network is UMTS */
            public static final int NETWORK_TYPE_UMTS = 3;
            /** Current network is CDMA: Either IS95A or IS95B*/
            public static final int NETWORK_TYPE_CDMA = 4;
            /** Current network is EVDO revision 0*/
            public static final int NETWORK_TYPE_EVDO_0 = 5;
            /** Current network is EVDO revision A*/
            public static final int NETWORK_TYPE_EVDO_A = 6;
            /** Current network is 1xRTT*/
            public static final int NETWORK_TYPE_1xRTT = 7;
            /** Current network is HSDPA */
            public static final int NETWORK_TYPE_HSDPA = 8;
            /** Current network is HSUPA */
            public static final int NETWORK_TYPE_HSUPA = 9;
            /** Current network is HSPA */
            public static final int NETWORK_TYPE_HSPA = 10;
            /** Current network is iDen */
            public static final int NETWORK_TYPE_IDEN = 11;
            /** Current network is EVDO revision B*/
            public static final int NETWORK_TYPE_EVDO_B = 12;
            /** Current network is LTE */
            public static final int NETWORK_TYPE_LTE = 13;
            /** Current network is eHRPD */
            public static final int NETWORK_TYPE_EHRPD = 14;
            /** Current network is HSPA+ */
            public static final int NETWORK_TYPE_HSPAP = 15;


        /**
         * 判断当前使用的手机网络的类型
         * @param context
         * @return
         */
        public  int  MobileNetTypeUtils(Context context){


             TelephonyManager telManager = (TelephonyManager) context   
                        .getSystemService(Context.TELEPHONY_SERVICE); 

            return getNetworkClass(telManager.getNetworkType());
        }

        /** Unknown network class. {@hide} */
        public static final int NETWORK_CLASS_UNKNOWN = 0;
        /** Class of broadly defined "2G" networks. {@hide} */
        public static final int NETWORK_CLASS_2_G = 1;
        /** Class of broadly defined "3G" networks. {@hide} */
        public static final int NETWORK_CLASS_3_G = 2;
        /** Class of broadly defined "4G" networks. {@hide} */
        public static final int NETWORK_CLASS_4_G = 3;

        /**
         * 判断手机网络2G\3G\4G
         * Return general class of network type, such as "3G" or "4G". In cases
         * where classification is contentious, this method is conservative.
         *
         * @hide
         */
        public  int getNetworkClass(int networkType) {
            switch (networkType) {
                case NETWORK_TYPE_GPRS:
                case NETWORK_TYPE_EDGE:
                case NETWORK_TYPE_CDMA:
                case NETWORK_TYPE_1xRTT:
                case NETWORK_TYPE_IDEN:
                    return NETWORK_CLASS_2_G;
                case NETWORK_TYPE_UMTS:
                case NETWORK_TYPE_EVDO_0:
                case NETWORK_TYPE_EVDO_A:
                case NETWORK_TYPE_HSDPA:
                case NETWORK_TYPE_HSUPA:
                case NETWORK_TYPE_HSPA:
                case NETWORK_TYPE_EVDO_B:
                case NETWORK_TYPE_EHRPD:
                case NETWORK_TYPE_HSPAP:
                    return NETWORK_CLASS_3_G;
                case NETWORK_TYPE_LTE:
                    return NETWORK_CLASS_4_G;
                default:
                    return NETWORK_CLASS_UNKNOWN;
            }
        }
}

 

posted @ 2018-09-01 10:30  云中雀  阅读(908)  评论(0编辑  收藏  举报