Android开发

Android 判断现有网络类型

时间:2016-5-20 11:11:46  作者:WPF之家  来源:http://www.wpf123.com/  查看:580  评论:0
内容摘要:public static String GetNetworkType(){ String strNetworkType = ""; NetworkInfo networkInfo = (ConnectivityManager) getSystemSe...
public static String GetNetworkType()
{
    String strNetworkType = "";
    
    NetworkInfo networkInfo = (ConnectivityManager) 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 = "2G";
                    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 = "3G";
                    break;
                case TelephonyManager.NETWORK_TYPE_LTE:    //api<11 : replace by 13                     strNetworkType = "4G";
                    break;
                default:
                    // http://baike.baidu.com/item/TD-SCDMA 中国移动 联通 电信 三种3G制式                     if (_strSubTypeName.equalsIgnoreCase("TD-SCDMA") || _strSubTypeName.equalsIgnoreCase("WCDMA") || _strSubTypeName.equalsIgnoreCase("CDMA2000")) 
                    {
                        strNetworkType = "3G";
                    }
                    else                     {
                        strNetworkType = _strSubTypeName;
                    }
                    
                    break;
             }
             
            Log.e("cocos2d-x", "Network getSubtype : " + Integer.valueOf(networkType).toString());
        }
    }
    
    Log.e("cocos2d-x", "Network Type : " + strNetworkType);
    
    return strNetworkType;
}

标签:Android 判断 现有 网络 类型 

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。本文出自“wpf之家”,请务必保留此出处:http://www.wpf123.com

上一篇:没有了
下一篇:sqlite导入大量数据耗时的解决方法
相关评论
Copyright © 2009-2014 WPF之家(http://www.wpf123.com/) All rights reserved
 Powered by WPF之家
鄂ICP备13006396号