SoFunction
Updated on 2025-04-07

Super practical android network tools

In actual development, some tool classes are often very helpful to us. In this way, based on the predecessors, I have compiled a network tool class and hereby presented it:

/**
  * @Class name:NetUtil
  * @Class Description: Network judgment processing class
  * @Created time: February 12, 2015-9:34:32 am
  * @Modified by:
  * @Modification time:
  * @Modify Notes:
  * @Version:
  */
public class NetUtil {
  /* Network status */
  public static boolean isNet = true;
  public static enum netType
  {
    wifi, CMNET, CMWAP, noneNet
  }

  /**
    * @Method Description: Determine whether the WIFI network is available
    * @Method Name: isWifiConnected
    * @param context
    * @return
    * @Return value:boolean
    */
  public static boolean isWifiConnected(Context context)
  {
    if (context != null)
    {
      ConnectivityManager mConnectivityManager = (ConnectivityManager) context
          .getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo mWiFiNetworkInfo = mConnectivityManager
          .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
      if (mWiFiNetworkInfo != null)
      {
        return ();
      }
    }
    return false;
  }

  /**
    * @Method Description: Determine whether the MOBILE network is available
    * @Method Name: isMobileConnected
    * @param context
    * @return
    * @Return value:boolean
    */
  public static boolean isMobileConnected(Context context)
  {
    if (context != null)
    {
      ConnectivityManager mConnectivityManager = (ConnectivityManager) context
          .getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo mMobileNetworkInfo = mConnectivityManager
          .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
      if (mMobileNetworkInfo != null)
      {
        return ();
      }
    }
    return false;
  }

  /**
    * @Method Description: Get the type information of the current network connection
    * @Method Name:getConnectedType
    * @param context
    * @return
    * @Return value:int
    */
  public static int getConnectedType(Context context)
  {
    if (context != null)
    {
      ConnectivityManager mConnectivityManager = (ConnectivityManager) context
          .getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo mNetworkInfo = mConnectivityManager
          .getActiveNetworkInfo();
      if (mNetworkInfo != null && ())
      {
        return ();
      }
    }
    return -1;
  }

  /**
    * @Method Description: Get the current network status -1: No network 1: WIFI network 2: wap network 3: net network
    * @Method Name: getAPNType
    * @param context
    * @return
    * @Return value:netType
    */
  public static netType getAPNType(Context context)
  {
    ConnectivityManager connMgr = (ConnectivityManager) context
        .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = ();
    if (networkInfo == null)
    {
      return ;
    }
    int nType = ();

    if (nType == ConnectivityManager.TYPE_MOBILE)
    {
      if (().toLowerCase().equals("cmnet"))
      {
        return ;
      }

      else
      {
        return ;
      }
    } else if (nType == ConnectivityManager.TYPE_WIFI)
    {
      return ;
    }
    return ;

  }

  /**
    * @Method Description: Determine whether there is a network connection
    * @Method Name: isNetworkConnected
    * @param context
    * @return
    * @Return value:boolean
    */
  public static boolean isNetworkConnected(Context context) {
    if (context != null) {
      ConnectivityManager mConnectivityManager = (ConnectivityManager) context
          .getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo mNetworkInfo = mConnectivityManager
          .getActiveNetworkInfo();
      if (mNetworkInfo != null) {
        return ();
      }
    }
    return false;
  }

  /**
    * @Method Description: Is the network available?
    * @Method Name: isNetworkAvailable
    * @param context
    * @return
    * @Return value:boolean
    */
  public static boolean isNetworkAvailable(Context context)
  {
    ConnectivityManager mgr = (ConnectivityManager) context
        .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] info = ();
    if (info != null)
    {
      for (int i = 0; i < ; i++)
      {
        if (info[i].getState() == )
        {
          return true;
        }
      }
    }
    return false;
  }

  /**
    * @Method Description: Determine whether it is a mobile network
    * @Method Name: is3GNet
    * @param context
    * @return
    * @Return value:boolean
    */
  public static boolean is3GNet(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
        .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetInfo = ();
    if (activeNetInfo != null
        && () == ConnectivityManager.TYPE_MOBILE) {
      return true;
    }
    return false;
  }
}

OK! Hope it will be helpful to everyone!

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.