I need to tell if my device has Internet connection or not. I found many answers like:
private boolean isNetworkAvailable() {
ConnectivityManager connect
You can do this with very simple class.
class CheckInternet {
fun isNetworkAvailable(context: Context): Boolean {
val connectivityManager =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager?
val activeNetworkInfo = connectivityManager!!.activeNetworkInfo
return activeNetworkInfo != null && activeNetworkInfo.isConnected
}
}
Now you can check this from any class.
if (CheckInternet().isNetworkAvailable(this)) {
//connected with internet
}else{
//Not connected with internet
}