Question

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.

Was it helpful?

Solution

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!

OTHER TIPS

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top