Android Short Codes

How To Check Internet Connection Using Code

Android Check Internet Connetion
Written by Yasir Ameen

Using ConnectivityManager and NetworkInfo class you can check Network Connection of your device. It is always recommended that you check network connectivity before executing any network related task.


Note : Make sure you have assign following permissions in your manifest file.

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

Following code snippet will return true if your device is connected to the network, otherwise false.

 private boolean isOnline() {

   ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
   NetworkInfo netInfo = cm.getActiveNetworkInfo();
   if (netInfo != null && netInfo.isConnectedOrConnecting()) {
          return true;
   else {
          return false;

How to use:

You can use above code snippet using the conditional statements.

  if (isOnline()) {
    //call when you are connected to the internet.
  else {
   //call when you are not connected to the internet.

About the author

Yasir Ameen

I'm a programmer, teacher, and speaker. I work out of my home in Pakistan, Karachi for the Mobile, especially Android Platform. I discuss about technology, gadgets, codes, the devices we’re going and we’ve been. I’m excited about community, social equity, and media.

1 Comment

Leave a Comment