DEV Community

Varun Barad
Varun Barad

Posted on • Originally published at varunbarad.com on

Check network connectivity on Android in 10 lines

Have you ever wanted to show different things to your user based on whether they are connected to the network or not? Well, Android provides a simple way to check for an active network connection.

Note: This is checking whether or not the device is connected to a network (and not whether it is connected to Internet or not).

The way Android provides to check for network connectivity is just 10 lines of code. But me being the lazy coder that I am, I just write that code inside a helper method once and then use that one-line method call everywhere that I need to check for connectivity.

As you can see in the code-block below, it is quite easy to wrap the connectivity check logic in a function.

// Java code sample

public class ConnectivityHelper {
  public static boolean isConnectedToNetwork(Context context) {
    ConnectivityManager connectivityManager =
        (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    boolean isConnected = false;
    if (connectivityManager != null) {
      NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
      isConnected = (activeNetwork != null) && (activeNetwork.isConnectedOrConnecting());
    }

    return isConnected;
  }
}

// Kotlin code sample

// The next line should be the first statement in the file
@file:JvmName("ConnectivityHelper") // This line is only needed if you don't want caller statement in Java to change

fun Context.isConnectedToNetwork(): Boolean {
  val connectivityManager = this.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager?
  return connectivityManager?.activeNetworkInfo?.isConnectedOrConnecting() ?: false
}

You also need to have the ACCESS_NETWORK_STATE permission added in your manifest like below.

<manifest ...>

  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <application ...>
    ...
  </application>
</manifest>

Done

Now anywhere that you need to check the connection, you only need to

// Java code sample
if (ConnectivityHelper.isConnectedToNetwork(context)) {
  // Show the connected screen
} else {
  // Show disconnected screen
}
// Kotlin code sample
if (context.isConnectedToNetwork()) {
  // Show the connected screenn
} else {
  // Show disconnected screen
}

Further reading

The Official Documentations are a good place to find more about this.

If you know of a better way for this, contact me or tweet to me @varun_barad. You can even send me ideas for any other topics that you would like to know about.

Notes

Hat-tip to Giorgos Neokleous for suggesting to create an extension function in Kotlin.

Top comments (4)

Collapse
 
dantenguyen1008 profile image
An Nguyen

activeNetworkInfo is deprecated in SDK 29.

Collapse
 
neokleoys2005 profile image
Giorgos Neokleous

The Kotlin snippet could be an extension function on the Context object.

It would be less verbose!

Collapse
 
varunbarad profile image
Varun Barad

That my friend is a good idea and I did it. Rewrote that part as an extension function on Context class.

Thank you 💛

Collapse
 
bryce666 profile image
Bryce666

all is always deprecated in sdk
LOL