I have been using ConnectivityManager to detect whether there is an internet connection or not however, it only tells you that regardless of if there is really an internet connectivity or not, i have heard this could be detected by sending pings to https://www.google.com/ . i would also like to show toasts.
ConnectivityManager cmanager = (ConnectivityManager)getSystemService(this.CONNECTIVITY_SERVICE);
NetworkInfo info = cmanager.getActiveNetworkInfo();
if(info!=null && info.isConnected()) {
if(info.getType() == ConnectivityManager.TYPE_WIFI) {
Toast.makeText(MainActivity.this, "Wifi", Toast.LENGTH_LONG).show();
} else if(info.getType() == ConnectivityManager.TYPE_MOBILE) {
Toast.makeText(MainActivity.this, "mobile", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(MainActivity.this, "Not connected", Toast.LENGTH_LONG).show();
}
}
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>