Put this method in the class you want to check connectivity:
public static boolean isOnline(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
return true;
}
return false;
}
Then when you need to check connection, do this (using your example):
if(isOnline(getApplicationContext()){
webview.loadUrl("http://the.url.com")
}
else{
Toast.makeText(context, text, duration).show()
}
You can also create that method in a class and always use it from there, like ExampleClass.isOnline().
Do not forget to add this to your AndroidManifest:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />