0

How to check if the internet connection is Enabled in Android? Like if the LOCATION SERVICE is disabled we use the technique

Location myLocation = locationmanager.getLastKnownLocation(provider);
if(myLocation == null){
      Show the AlertDialog.
}else{
do this.
}

Like this, how do i check if the Internet connection is Enabled/Disabled.

Hasib Hasan
  • 93
  • 1
  • 2
  • 10
  • may be this link can help u http://stackoverflow.com/questions/4238921/android-detect-whether-there-is-an-internet-connection-available – amit kumar Mar 31 '14 at 16:24

2 Answers2

2

For wifi:

ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = mgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
connected = networkInfo != null && networkInfo.isConnected();

For Mobile:

ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = mgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
connected = networkInfo != null && networkInfo.isConnected();
Guillermo Merino
  • 3,197
  • 2
  • 17
  • 34
2

You can use the following method:

ConnectivityManager conectivtyManager = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);  
if (conectivtyManager.getActiveNetworkInfo() != null  
    && conectivtyManager.getActiveNetworkInfo().isAvailable()  
    && conectivtyManager.getActiveNetworkInfo().isConnected()) {  
    isConnected = true;  
} else {  
    isConnected= false;  
}  

Hope it helps!

Giacomoni
  • 1,468
  • 13
  • 18