SoFunction
Updated on 2025-03-02

Android determines whether the network is available and whether the connected network can access the Internet

Network status acquisition

Both upload and download need to check the current network status of the phone first, and you need to obtain the ConnectionManager

 /**
  * Determine whether there is currently a network connection, but if the connected network cannot access the Internet, it will also return true.
  * @param mContext
  * @return
  */
 public static boolean isNetConnection(Context mContext) {
 if (mContext!=null){
  ConnectivityManager connectivityManager = (ConnectivityManager) (Context.CONNECTIVITY_SERVICE);
  NetworkInfo networkInfo = ();
  boolean connected = ();
  if (networkInfo!=null&&connected){
   if (()== ){
    return true;
   }else{
    return false;
   }
  }
 }
 return false;
}

Can the Internet be accessed normally?

When there is a network connection, if you want to check whether the currently connected network can access the Internet, you need to open the URL to make a judgment

/**
  * Enable this method in the child thread to detect whether the current network can open the web page.
  * true is available for internet access, false is available for internet access
  *
  */
public static boolean isOnline(){
 URL url;
 try {
  url = new URL("");
  InputStream stream = ();
  return true;
 } catch (MalformedURLException e) {
  ();
 } catch (IOException e) {
  ();
 }
 return false;
}

The above article on Android: judging whether the network is available and whether the connected network can be accessed online is all the content I have shared with you. I hope you can give you a reference and I hope you can support me more.