Detailed Explanation of Android Network Tool Class NetworkUtils

  • 2021-09-05 00:48:49
  • OfStack

Network tool class NetworkUtils, for your reference, the specific contents are as follows

Provided methods:

Open the network setting interface openWirelessSettings
Determine whether the network is available with isAvailable
Determine whether the network is connected to isConnected
Determining whether the network is 4G is4G
Whether the wifi is connected to the isWifiConnected is judged
Get the mobile network operator name getNetworkOperatorName
Get the mobile terminal type getPhoneType
Get the current network type (WIFI, 2G, 3G, 4G) getNetWorkType, getNetWorkTypeName

Code:


import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

/**
 *  Network tool class 
 */
public class NetworkUtils {

  public static final int NETWORK_NO = -1;  // no network
  public static final int NETWORK_WIFI = 1;  // wifi network
  public static final int NETWORK_2G = 2;  // "2G" networks
  public static final int NETWORK_3G = 3;  // "3G" networks
  public static final int NETWORK_4G = 4;  // "4G" networks
  public static final int NETWORK_UNKNOWN = 5;  // unknown network

  private static final int NETWORK_TYPE_GSM = 16;
  private static final int NETWORK_TYPE_TD_SCDMA = 17;
  private static final int NETWORK_TYPE_IWLAN = 18;

  /**
   *  Open the network setting interface 
   * <p>3.0 Open the settings interface below </p>
   *
   * @param context  Context 
   */
  public static void openWirelessSettings(Context context) {
    if (android.os.Build.VERSION.SDK_INT > 10) {
      context.startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
    } else {
      context.startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
    }
  }

  /**
   *  Obtain active network information 
   *
   * @param context  Context 
   * @return NetworkInfo
   */
  private static NetworkInfo getActiveNetworkInfo(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context
        .getSystemService(Context.CONNECTIVITY_SERVICE);
    return cm.getActiveNetworkInfo();
  }

  /**
   *  Determine whether the network is available 
   * <p> Need to add permissions  {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>}</p>
   *
   * @param context  Context 
   * @return {@code true}:  Available <br>{@code false}:  Not available 
   */
  public static boolean isAvailable(Context context) {
    NetworkInfo info = getActiveNetworkInfo(context);
    return info != null && info.isAvailable();
  }

  /**
   *  Determine whether the network is connected or not 
   * <p> Need to add permissions  {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>}</p>
   *
   * @param context  Context 
   * @return {@code true}:  Yes <br>{@code false}:  No 
   */
  public static boolean isConnected(Context context) {
    NetworkInfo info = getActiveNetworkInfo(context);
    return info != null && info.isConnected();
  }

  /**
   *  Determine whether the network is 4G
   * <p> Need to add permissions  {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>}</p>
   *
   * @param context  Context 
   * @return {@code true}:  Yes <br>{@code false}:  No 
   */
  public static boolean is4G(Context context) {
    NetworkInfo info = getActiveNetworkInfo(context);
    return info != null && info.isAvailable() && info.getSubtype() == TelephonyManager.NETWORK_TYPE_LTE;
  }

  /**
   *  Judge wifi Connection status 
   * <p> Need to add permissions  {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>}</p>
   *
   * @param context  Context 
   * @return {@code true}:  Connect <br>{@code false}:  Not connected 
   */
  public static boolean isWifiConnected(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context
        .getSystemService(Context.CONNECTIVITY_SERVICE);
    return cm != null && cm.getActiveNetworkInfo() != null
        && cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI;
  }

  /**
   *  Get the mobile network operator name 
   * <p> Such as China Unicom, China Mobile and China Telecom </p>
   *
   * @param context  Context 
   * @return  Name of mobile network operator 
   */
  public static String getNetworkOperatorName(Context context) {
    TelephonyManager tm = (TelephonyManager) context
        .getSystemService(Context.TELEPHONY_SERVICE);
    return tm != null ? tm.getNetworkOperatorName() : null;
  }

  /**
   *  Get the mobile terminal type 
   *
   * @param context  Context 
   * @return  Mobile phone system 
   * <ul>
   * <li>{@link TelephonyManager#PHONE_TYPE_NONE } : 0  Unknown mobile phone system </li>
   * <li>{@link TelephonyManager#PHONE_TYPE_GSM } : 1  The mobile phone system is GSM Mobile and Unicom </li>
   * <li>{@link TelephonyManager#PHONE_TYPE_CDMA } : 2  The mobile phone system is CDMA Telecommunications </li>
   * <li>{@link TelephonyManager#PHONE_TYPE_SIP } : 3</li>
   * </ul>
   */
  public static int getPhoneType(Context context) {
    TelephonyManager tm = (TelephonyManager) context
        .getSystemService(Context.TELEPHONY_SERVICE);
    return tm != null ? tm.getPhoneType() : -1;
  }

  /**
   *  Get the current network type (WIFI,2G,3G,4G)
   * <p> Need to add permissions  {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>}</p>
   *
   * @param context  Context 
   * @return  Network type 
   * <ul>
   * <li>{@link #NETWORK_WIFI  } = 1;</li>
   * <li>{@link #NETWORK_4G   } = 4;</li>
   * <li>{@link #NETWORK_3G   } = 3;</li>
   * <li>{@link #NETWORK_2G   } = 2;</li>
   * <li>{@link #NETWORK_UNKNOWN} = 5;</li>
   * <li>{@link #NETWORK_NO   } = -1;</li>
   * </ul>
   */
  public static int getNetWorkType(Context context) {
    int netType = NETWORK_NO;
    NetworkInfo info = getActiveNetworkInfo(context);
    if (info != null && info.isAvailable()) {

      if (info.getType() == ConnectivityManager.TYPE_WIFI) {
        netType = NETWORK_WIFI;
      } else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
        switch (info.getSubtype()) {

          case NETWORK_TYPE_GSM:
          case TelephonyManager.NETWORK_TYPE_GPRS:
          case TelephonyManager.NETWORK_TYPE_CDMA:
          case TelephonyManager.NETWORK_TYPE_EDGE:
          case TelephonyManager.NETWORK_TYPE_1xRTT:
          case TelephonyManager.NETWORK_TYPE_IDEN:
            netType = NETWORK_2G;
            break;

          case NETWORK_TYPE_TD_SCDMA:
          case TelephonyManager.NETWORK_TYPE_EVDO_A:
          case TelephonyManager.NETWORK_TYPE_UMTS:
          case TelephonyManager.NETWORK_TYPE_EVDO_0:
          case TelephonyManager.NETWORK_TYPE_HSDPA:
          case TelephonyManager.NETWORK_TYPE_HSUPA:
          case TelephonyManager.NETWORK_TYPE_HSPA:
          case TelephonyManager.NETWORK_TYPE_EVDO_B:
          case TelephonyManager.NETWORK_TYPE_EHRPD:
          case TelephonyManager.NETWORK_TYPE_HSPAP:
            netType = NETWORK_3G;
            break;

          case NETWORK_TYPE_IWLAN:
          case TelephonyManager.NETWORK_TYPE_LTE:
            netType = NETWORK_4G;
            break;
          default:

            String subtypeName = info.getSubtypeName();
            if (subtypeName.equalsIgnoreCase("TD-SCDMA")
                || subtypeName.equalsIgnoreCase("WCDMA")
                || subtypeName.equalsIgnoreCase("CDMA2000")) {
              netType = NETWORK_3G;
            } else {
              netType = NETWORK_UNKNOWN;
            }
            break;
        }
      } else {
        netType = NETWORK_UNKNOWN;
      }
    }
    return netType;
  }

  /**
   *  Get the current network type (WIFI,2G,3G,4G)
   * <p> Depend on the above method </p>
   *
   * @param context  Context 
   * @return  Network type name 
   * <ul>
   * <li>NETWORK_WIFI  </li>
   * <li>NETWORK_4G   </li>
   * <li>NETWORK_3G   </li>
   * <li>NETWORK_2G   </li>
   * <li>NETWORK_UNKNOWN</li>
   * <li>NETWORK_NO   </li>
   * </ul>
   */
  public static String getNetWorkTypeName(Context context) {
    switch (getNetWorkType(context)) {
      case NETWORK_WIFI:
        return "NETWORK_WIFI";
      case NETWORK_4G:
        return "NETWORK_4G";
      case NETWORK_3G:
        return "NETWORK_3G";
      case NETWORK_2G:
        return "NETWORK_2G";
      case NETWORK_NO:
        return "NETWORK_NO";
      default:
        return "NETWORK_UNKNOWN";
    }
  }
}

Related articles: