Check Internet Availability in Android Devices

The getActiveNetworkInfo() method of ConnectivityManager returns a NetworkInfoinstance representing the first connected network interface it can find or null if none if the interfaces are connected. Checking if this method returns null should be enough to tell if an internet connection is available.

privateboolean isNetworkAvailable(){
   ConnectivityManager connectivityManager 
          =(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
   NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
  return activeNetworkInfo !=null&& activeNetworkInfo.isConnected();
}

in your android manifest.

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

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s