SoFunction
Updated on 2025-04-09

Android network tool class NetworkUtils detailed explanation

NetworkUtils, a network tool class, is for your reference. The specific content is as follows

Methods provided:

Open the network settings interface openWirelessSettings
Determine whether the network is available isAvailable
Determine whether the network is connected isConnected
Determine whether the network is 4G is4G
Determine whether wifi is connected to the status of the connection isWifiConnected
Get the name of the mobile network operator getNetworkOperatorName
Get mobile terminal type getPhoneType
Get the current network type (WIFI, 2G, 3G, 4G) getNetWorkType, getNetWorkTypeName

Code:

import ;
import ;
import ;
import ;
import ;

/**
  * 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 settings interface
    * <p>The settings interface is opened below 3.0</p>
    *
    * @param context context
    */
  public static void openWirelessSettings(Context context) {
    if (.SDK_INT &gt; 10) {
      (new Intent(.ACTION_SETTINGS));
    } else {
      (new Intent(.ACTION_WIRELESS_SETTINGS));
    }
  }

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

  /**
    * Determine whether the network is available
    * <p>Permission required {@code <uses-permission android:name=".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 &amp;&amp; ();
  }

  /**
    * Determine whether the network is connected
    * <p>Permission required {@code <uses-permission android:name=".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 &amp;&amp; ();
  }

  /**
    * Determine whether the network is 4G
    * <p>Permission required {@code <uses-permission android:name=".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 &amp;&amp; () &amp;&amp; () == TelephonyManager.NETWORK_TYPE_LTE;
  }

  /**
    * Determine whether the wifi is connected
    * <p>Permission required {@code <uses-permission android:name=".ACCESS_NETWORK_STATE"/>}</p>
    *
    * @param context context
    * @return {@code true}: Connection<br>{@code false}: Not connected
    */
  public static boolean isWifiConnected(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context
        .getSystemService(Context.CONNECTIVITY_SERVICE);
    return cm != null &amp;&amp; () != null
        &amp;&amp; ().getType() == ConnectivityManager.TYPE_WIFI;
  }

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

  /**
    * Get the mobile terminal type
    *
    * @param context context
    * @return Mobile phone
    * <ul>
    * <li>{@link TelephonyManager#PHONE_TYPE_NONE } : 0 Mobile phone format unknown</li>
    * <li>{@link TelephonyManager#PHONE_TYPE_GSM } : 1 The mobile phone format is GSM, Mobile and China Unicom</li>
    * <li>{@link TelephonyManager#PHONE_TYPE_CDMA } : 2 The mobile phone standard is CDMA, Telecom</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 ? () : -1;
  }

  /**
    * Get the current network type (WIFI, 2G, 3G, 4G)
    * <p>Permission required {@code <uses-permission android:name=".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 &amp;&amp; ()) {

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

          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 = ();
            if (("TD-SCDMA")
                || ("WCDMA")
                || ("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";
    }
  }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.