You can use following snippet to check Internet Connection.
It will useful both way that you can check which Type of NETWORK
Connection is available so you can do your process on that way.
You just have to copy following class and paste directly in your package.
/**
* @author Pratik Butani
*/
public class InternetConnection {
/**
* CHECK WHETHER INTERNET CONNECTION IS AVAILABLE OR NOT
*/
public static boolean checkConnection(Context context) {
final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connMgr != null) {
NetworkInfo activeNetworkInfo = connMgr.getActiveNetworkInfo();
if (activeNetworkInfo != null) { // connected to the internet
// connected to the mobile provider's data plan
if (activeNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
// connected to wifi
return true;
} else return activeNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE;
}
}
return false;
}
}
Now you can use like:
if (InternetConnection.checkConnection(context)) {
// Its Available...
} else {
// Not Available...
}
DON'T FORGET to TAKE Permission :) :)
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
You can modify based on your requirement.
Thank you.