Ultra practical android network tool class

  • 2021-09-05 00:47:56
  • OfStack

In the actual development, one tool class is often of great help to us. Therefore, on the basis of predecessors, I have sorted out one network tool class, and hereby offer it:


/**
 * @ Class name :NetUtil
 * @ Class description : Network judgment processing class 
 * @ Creation time :2015 Year 2 Month 12 Day - Morning 9:34:32
 * @ Modifier :
 * @ Modification time :
 * @ Modify comments :
 * @ Version :
 */
public class NetUtil {
  /*  Network state  */
  public static boolean isNet = true;
  public static enum netType
  {
    wifi, CMNET, CMWAP, noneNet
  }

  /**
   * @ Method description : Judge WIFI Is the network 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 mWiFiNetworkInfo.isAvailable();
      }
    }
    return false;
  }

  /**
   * @ Method description : Judge MOBILE Is the network 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 mMobileNetworkInfo.isAvailable();
      }
    }
    return false;
  }

  /**
   * @ Method description : Gets 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 && mNetworkInfo.isAvailable())
      {
        return mNetworkInfo.getType();
      }
    }
    return -1;
  }

  /**
   * @ Method description : Get the current network state  -1 : There's no internet access  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 = connMgr.getActiveNetworkInfo();
    if (networkInfo == null)
    {
      return netType.noneNet;
    }
    int nType = networkInfo.getType();

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

      else
      {
        return netType.CMWAP;
      }
    } else if (nType == ConnectivityManager.TYPE_WIFI)
    {
      return netType.wifi;
    }
    return netType.noneNet;

  }

  /**
   * @ 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 mNetworkInfo.isAvailable();
      }
    }
    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 = mgr.getAllNetworkInfo();
    if (info != null)
    {
      for (int i = 0; i < info.length; i++)
      {
        if (info[i].getState() == NetworkInfo.State.CONNECTED)
        {
          return true;
        }
      }
    }
    return false;
  }

  /**
   * @ Method description : Judge whether it is a mobile phone 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 = connectivityManager.getActiveNetworkInfo();
    if (activeNetInfo != null
        && activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
      return true;
    }
    return false;
  }
}

Okay! Hope to help everyone!


Related articles: