android 中 我们经常需要判断 各种网络类型 然后需要给用户做出相应的提示,一般网络类型有 2g ,3g,4g ,wifi,无网络等等。
1. 网络类型判断
TelephonyManager 中 定义常量值
public static final int NETWORK_TYPE_1xRTT = 7; public static final int NETWORK_TYPE_CDMA = 4; public static final int NETWORK_TYPE_EDGE = 2; public static final int NETWORK_TYPE_EHRPD = 14; public static final int NETWORK_TYPE_EVDO_0 = 5; public static final int NETWORK_TYPE_EVDO_A = 6; public static final int NETWORK_TYPE_EVDO_B = 12; public static final int NETWORK_TYPE_GPRS = 1; public static final int NETWORK_TYPE_HSDPA = 8; public static final int NETWORK_TYPE_HSPA = 10; public static final int NETWORK_TYPE_HSPAP = 15; public static final int NETWORK_TYPE_HSUPA = 9; public static final int NETWORK_TYPE_IDEN = 11; public static final int NETWORK_TYPE_LTE = 13; public static final int NETWORK_TYPE_UMTS = 3; public static final int NETWORK_TYPE_UNKNOWN = 0;
public static int getNetworkType(Context context) { ConnectivityManager connectMgr = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE); NetworkInfo info = connectMgr.getActiveNetworkInfo(); if (info != null) { if (info.getType() == ConnectivityManager.TYPE_MOBILE) { switch (info.getSubtype()) { case TelephonyManager.NETWORK_TYPE_CDMA: // telcom case TelephonyManager.NETWORK_TYPE_1xRTT: // telecom case TelephonyManager.NETWORK_TYPE_GPRS: // unicom case TelephonyManager.NETWORK_TYPE_EDGE: // cmcc return NETWORK_TYPE_2G; case TelephonyManager.NETWORK_TYPE_EHRPD: // telecom case TelephonyManager.NETWORK_TYPE_EVDO_0: // telecom case TelephonyManager.NETWORK_TYPE_EVDO_A: // telecom 3.5G case TelephonyManager.NETWORK_TYPE_EVDO_B: // telecom 3.5G case TelephonyManager.NETWORK_TYPE_HSPA: // unicom case TelephonyManager.NETWORK_TYPE_HSPAP: // unicom case TelephonyManager.NETWORK_TYPE_HSDPA: // unicom 3.5G case TelephonyManager.NETWORK_TYPE_HSUPA: // unicom 3.5G case TelephonyManager.NETWORK_TYPE_UMTS: // unicom return NETWORK_TYPE_3G; case TelephonyManager.NETWORK_TYPE_LTE: return NETWORK_TYPE_4G; } } else if (info.getType() == ConnectivityManager.TYPE_WIFI) { return NETWORK_TYPE_WIFI; } } return NETWORK_TYPE_NONE; }
public static final int NETWORK_TYPE_NONE = 0; public static final int NETWORK_TYPE_2G = 2; public static final int NETWORK_TYPE_3G = 3; public static final int NETWORK_TYPE_4G = 4; public static final int NETWORK_TYPE_WIFI = 10; private String getNetType() { int type = getNetworkType(mContext); String netType = ""; switch (type) { case Utils.NETWORK_TYPE_NONE: netType = ""; break; case Utils.NETWORK_TYPE_2G: netType = "2G"; break; case Utils.NETWORK_TYPE_3G: netType = "3G"; break; case Utils.NETWORK_TYPE_4G: netType = "4G"; break; case Utils.NETWORK_TYPE_WIFI: netType = "wifi"; break; } return netType; }
2.实时监听网络切换
实现监听网络的功能主要依靠 广播监听 。不多说 看代码。回调中逻辑改成自己的就可以了。
oncreate中 需要注册一下 广播 mNetworkReceiver = new NetTypeReceiver(); registerReceiver(mNetworkReceiver, intent); ondestory中需要将广播销毁 if (mNetworkReceiver != null) { unregisterReceiver(mNetworkReceiver); mNetworkReceiver = null; } private class NetTypeReceiver extends BroadcastReceiver { String type = ""; @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) { ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService( Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.isAvailable()) { String name = networkInfo.getTypeName(); switch (networkInfo.getType()) { case 0://移动 网络 if (TextUtils.isEmpty(type)) { type = name; } break; case 1:wifi网络 if (TextUtils.isEmpty(type)) { type = name; } break; } } else {// 无网络 //network was unavailable, stop the streamer, and show a dialog showNetworkInvalidView(R.string.network_error_message); type = ""; } } } }