关闭

网络判断

88人阅读 评论(0) 收藏 举报
分类:
  • 工具类
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

public class NetWorkUtils {
    public static final int G3 = 3;// 3G
    public static final int G2 = 2;// 2G
    public static final int G4 = 4;// 4G
    public static final int WIFI = 5;// WIFI
    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;
                }

            }
        }

        return strNetworkType;
    }
}
  • 调用
private void wangLuoPanDuan() {
        // TODO Auto-generated method stub
        int type = NetWorkUtils.GetNetworkType(Login.this);


        if(NetWorkUtils.WIFI == type){
            Toast.makeText(Login.this, "WIFI", 0).show();
        }else if(NetWorkUtils.G4 == type){
            Toast.makeText(Login.this, "4G", 0).show();
        }else if(NetWorkUtils.G3 == type){
            Toast.makeText(Login.this, "3G", 0).show();
        }else if(NetWorkUtils.G2 == type){
            Toast.makeText(Login.this, "2G", 0).show();
        }else if(NetWorkUtils.NO == type){
            Toast.makeText(Login.this, "锟斤拷锟斤拷锟斤拷", 0).show();
        }
  • 权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2571次
    • 积分:197
    • 等级:
    • 排名:千里之外
    • 原创:18篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类