网络判断

  • 工具类
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"/>
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/baidu_35124863/article/details/51559294
个人分类: Android
上一篇友盟第三方推送
下一篇自定义ImageView画圆
想对作者说点什么? 我来说一句

网络判断demo

2014年08月15日 1.38MB 下载

网络判断,界面启动

2017年07月07日 1.72MB 下载

android 网络判断

2013年10月26日 46KB 下载

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

关闭
关闭