判断网络类型

判断网络类型

public static final int G3 = 3;

    public static final int G2 = 2;
    public static final int G4 = 4;
    public static final int WIFI = 5;
    public static final int NO = 0;
    
    public static int GetNetworkType(Context context) {
        int strNetworkType = NO;
        NetworkInfo networkInfo = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE))
                .getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                strNetworkType = WIFI;
            } else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                String _strSubTypeName = networkInfo.getSubtypeName();

                // Log.e("cocos2d-x", "Network getSubtypeName : " +
                // _strSubTypeName);

                // TD-SCDMA networkType is 17
                int networkType = networkInfo.getSubtype();
                switch (networkType) {
                case TelephonyManager.NETWORK_TYPE_GPRS:
                case TelephonyManager.NETWORK_TYPE_EDGE:
                case TelephonyManager.NETWORK_TYPE_CDMA:
                case TelephonyManager.NETWORK_TYPE_1xRTT:
                case TelephonyManager.NETWORK_TYPE_IDEN: // api<8 : replace by
                                                            // 11
                    strNetworkType = G2;
                    break;
                case TelephonyManager.NETWORK_TYPE_UMTS:
                case TelephonyManager.NETWORK_TYPE_EVDO_0:
                case TelephonyManager.NETWORK_TYPE_EVDO_A:
                case TelephonyManager.NETWORK_TYPE_HSDPA:
                case TelephonyManager.NETWORK_TYPE_HSUPA:
                case TelephonyManager.NETWORK_TYPE_HSPA:
                case TelephonyManager.NETWORK_TYPE_EVDO_B: // api<9 : replace by
                                                            // 14
                case TelephonyManager.NETWORK_TYPE_EHRPD: // api<11 : replace by
                                                            // 12
                case TelephonyManager.NETWORK_TYPE_HSPAP: // api<13 : replace by
                                                            // 15
                    strNetworkType = G3;
                    break;
                case TelephonyManager.NETWORK_TYPE_LTE: // api<11 : replace by
                                                        // 13
                    strNetworkType = G4;
                    break;
                default:
                    // http://baike.baidu.com/item/TD-SCDMA 中国移动 联通 电信 三种3G制式
                    if (_strSubTypeName.equalsIgnoreCase("TD-SCDMA")
                            || _strSubTypeName.equalsIgnoreCase("WCDMA")
                            || _strSubTypeName.equalsIgnoreCase("CDMA2000")) {
                        strNetworkType = G3;
                    } else {
                        strNetworkType = NO;
                    }

                    break;
                }

                // Log.e("cocos2d-x", "Network getSubtype : " +
                // Integer.valueOf(networkType).toString());
            }
        }

        // Log.e("cocos2d-x", "Network Type : " + strNetworkType);

        return strNetworkType;
    }
判断方法

types = NetWorkUtils.GetNetworkType(getActivity());

if (types == 0) {
Toast.makeText(getActivity(), "没网", 0).show();

} else if (types == 5) {
Toast.makeText(getActivity(), "wifi", 0).show();
getdatas();
} else {
Toast.makeText(getActivity(), "G2,G3,G4", 0).show();
getdatas();
}


android.permission.ACCESS_NETWORK_STATE
允许程序访问有关的网络信息

android.permission.ACCESS_WIFI_STATE
允许程序访问Wi-Fi网络状态信息


阅读更多
想对作者说点什么? 我来说一句

Phonegap判断网络类型及状态

2014年03月28日 3.01MB 下载

没有更多推荐了,返回首页

不良信息举报

判断网络类型

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭