SoFunction
Updated on 2025-03-11

Detailed explanation of Android's network status method

Detailed explanation of Android's network status method

1. Join the network permission

To obtain network information, you need to add corresponding permissions to the file.

<uses-permission Android:name=".ACCESS_NETWORK_STATE" />

2. Several solutions to judge the mobile network

1) Determine whether there is a network connection

public 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;  
} 

2) Determine whether the WIFI network is available

public 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;  
} 

4) Obtain the type information of the current network connection

public static int getConnectedType(Context context) {  
  if (context != null) {  
    ConnectivityManager mConnectivityManager = (ConnectivityManager) context  
        .getSystemService(Context.CONNECTIVITY_SERVICE);  
    NetworkInfo mNetworkInfo = ();  
    if (mNetworkInfo != null && ()) {  
      return ();  
    }  
  }  
  return -1;  
} 

When developing Android applications, it involves network access, and network status checks are often required to provide users with necessary reminders. This work can generally be done through the ConnectivityManager.

ConnectivityManager has four main tasks:

1. Monitor the network status of the mobile phone (including GPRS, WIFI, UMTS, etc.)
2. When the mobile phone status changes, send a broadcast
3. Failover when a network connection fails
4. Provide applications with high-precision and rough states that can obtain available networks

When we want to monitor the network status in the program, we only need a few steps:

1. Define a Receiver overloading the onReceive function in it to complete the required functions, such as changing the appearance of the space based on whether WIFI and GPRS are disconnected.

connectionReceiver = new BroadcastReceiver() { 
   
  @Override 
  public void onReceive(Context context, Intent intent) { 
  ConnectivityManager connectMgr = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); 
  NetworkInfo mobNetInfo = (ConnectivityManager.TYPE_MOBILE); 
  NetworkInfo wifiNetInfo = (ConnectivityManager.TYPE_WIFI); 
 
  if (!() &amp;&amp; !()) { 
   (TAG, "unconnect"); 
   // unconnect network prompts the user to disconnect the network information   }else { 
 
  // connect network can make some network requests and refresh the interface   } 
  } 
 }; 

2. Register Receiver in the appropriate place. You can register it in the program and call the following functions in onCreate.

IntentFilter intentFilter = new IntentFilter(); 
 (ConnectivityManager.CONNECTIVITY_ACTION); 
 registerReceiver(connectionReceiver, intentFilter); 

3. Unregister Receiver when appropriate, you can cancel it in the program and call the following function in onDestroye:

if (connectionReceiver != null) { 
  unregisterReceiver(connectionReceiver); 
 } 

Having said so much. In fact, it mainly uses ConnectivityManager to obtain the current network status or listen for changes in network status. In this way, when the application needs to be connected to the network, the network status changes and can prompt the user in time, or automatically obtain network data and refresh when the network is reconnected.

Thank you for reading, I hope it can help you. Thank you for your support for this site!