一个判断网络类型的工具类(wifi 2g 3g 4g)

来源:互联网 发布:linux有什么浏览器 编辑:程序博客网 时间:2024/05/16 17:14
package com.example.test;import android.content.Context;import android.net.ConnectivityManager;import android.net.NetworkInfo;import android.telephony.TelephonyManager;import android.text.TextUtils;import android.widget.Toast;import static android.telephony.TelephonyManager.NETWORK_TYPE_1xRTT;import static android.telephony.TelephonyManager.NETWORK_TYPE_CDMA;import static android.telephony.TelephonyManager.NETWORK_TYPE_EDGE;import static android.telephony.TelephonyManager.NETWORK_TYPE_EHRPD;import static android.telephony.TelephonyManager.NETWORK_TYPE_EVDO_0;import static android.telephony.TelephonyManager.NETWORK_TYPE_EVDO_A;import static android.telephony.TelephonyManager.NETWORK_TYPE_EVDO_B;import static android.telephony.TelephonyManager.NETWORK_TYPE_GPRS;import static android.telephony.TelephonyManager.NETWORK_TYPE_HSDPA;import static android.telephony.TelephonyManager.NETWORK_TYPE_HSPA;import static android.telephony.TelephonyManager.NETWORK_TYPE_HSPAP;import static android.telephony.TelephonyManager.NETWORK_TYPE_HSUPA;import static android.telephony.TelephonyManager.NETWORK_TYPE_IDEN;import static android.telephony.TelephonyManager.NETWORK_TYPE_LTE;import static android.telephony.TelephonyManager.NETWORK_TYPE_UMTS;import static android.telephony.TelephonyManager.NETWORK_TYPE_UNKNOWN;/** * Created by liaoli on 15-11-30. */public class NetUtils {    private static final int NETWORKTYPE_UNKNOWN = -1;    /** 没有网络 */    public static final int NETWORKTYPE_INVALID = 0;    /** wap网络 */    public static final int NETWORKTYPE_WAP = 1;    /** 2G网络 */    public static final int NETWORKTYPE_2G = 2;    /** 3G3G以上网络,或统称为快速网络 */    public static final int NETWORKTYPE_3G = 3;    public static final int NETWORKTYPE_4G = 4;    /** wifi网络 */    public static final int NETWORKTYPE_WIFI = 5;    /**     * 获取网络类型     * @param context     * @return     */    public static int getNetWorkType(Context context) {        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);        NetworkInfo networkInfo = manager.getActiveNetworkInfo();        int mNetWorkType = NETWORKTYPE_INVALID;        if (networkInfo != null && networkInfo.isConnected()) {            String type = networkInfo.getTypeName();            if (type.equalsIgnoreCase("WIFI")) {                mNetWorkType = NETWORKTYPE_WIFI;            } else if (type.equalsIgnoreCase("MOBILE")) {                String proxyHost = android.net.Proxy.getDefaultHost();                if (!TextUtils.isEmpty(proxyHost)) {                    mNetWorkType = NETWORKTYPE_WAP;                }else{                    mNetWorkType = getCellarNetWorkType(context);                }            }        } else {            mNetWorkType = NETWORKTYPE_INVALID;        }        return mNetWorkType;    }    /**     * 获取手机的网络类型 2g 3g 4g      * @param context     * @return     */    public static int getCellarNetWorkType(Context context) {        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);        Toast.makeText(context, "" + telephonyManager.getNetworkType(), Toast.LENGTH_SHORT).show();        int type = NETWORKTYPE_INVALID;        switch (telephonyManager.getNetworkType()) {            case NETWORK_TYPE_1xRTT:                // ~ 50-100 kbps            case NETWORK_TYPE_CDMA:                // ~ 14-64 kbps            case NETWORK_TYPE_EDGE:                // ~ 50-100 kbps            case NETWORK_TYPE_GPRS:                // ~ 100 kbps            case NETWORK_TYPE_IDEN:                // ~25 kbps                type = NETWORKTYPE_2G;                break;            case NETWORK_TYPE_EVDO_0:                // ~ 400-1000 kbps            case NETWORK_TYPE_EVDO_A:                // ~ 600-1400 kbps            case NETWORK_TYPE_HSDPA:                // ~ 2-14 Mbps            case NETWORK_TYPE_HSPA:                // ~ 700-1700 kbps            case NETWORK_TYPE_HSUPA:                // ~ 1-23 Mbps            case NETWORK_TYPE_UMTS:                // ~ 400-7000 kbps            case NETWORK_TYPE_EHRPD:                // ~ 1-2 Mbps            case NETWORK_TYPE_EVDO_B:                // ~ 5 Mbps            case NETWORK_TYPE_HSPAP:                // ~ 10-20 Mbps                type = NETWORKTYPE_3G;                break;            case NETWORK_TYPE_LTE:// ~ 10+ Mbps                type = NETWORKTYPE_4G;                break;            case NETWORK_TYPE_UNKNOWN:                type = NETWORKTYPE_UNKNOWN;                break;            default:                break;        }        return type;    }}
0 0
原创粉丝点击