How to check if the Internet connection (Wifi/Mobile data) is ENABLED?

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.

Answers


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!


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();

Need Your Help

MATCH many rows against many rows within SQL query and find percentile match?

mysql sql database codeigniter active-record-query

At the moment I'm building a recruitment platform. An employer can post a job and receive applications. The employer can set many skill requirements which a jobseeker has to match. The jobseeker ca...

Dynamically calling object properties

javascript jquery

I have an object which is a member of an array called parameters , and the this object has a property called item_ , now if I assign a value to that property , how can I call it ?