Tuesday, July 14, 2020

How to check Internet Connection in Android

How to check Internet is Connected or not In Android


In Android Internet Connection can be checked by using  ConnectivityManager class.

Note : to use this you need to declare ACCESS_NETWORK_STATE 


Android Manifest

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



CODE :

boolean isInternetConnected()
{

   ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE );

   NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
   boolean isConncted = activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting();

   return isConncted;
}

No comments:

Post a Comment