Tuesday, November 24, 2020

Code to Check Internet Connection in Android 8 and above

 Code to Check Internet Connection in Android 8 and above




public boolean isInternetConnected(Context context) {
int result = 0; // Returns connection type. 0: none; 1: mobile data; 2: wifi
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (cm != null) {
NetworkCapabilities capabilities = cm.getNetworkCapabilities(cm.getActiveNetwork());
if (capabilities != null) {
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
return true;
} else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
return true;
} else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) {
return true;
}
}
}
} else {
if (cm != null) {
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null) {
// connected to the internet
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
return true;
} else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
return true;
} else if (activeNetwork.getType() == ConnectivityManager.TYPE_VPN) {
return true;
}
}
}
}
return false;
}

1 comment:


  1. To hire our Escorts Service in Mahipalpur you can speak with our call girl representative over the call. It is very easy to get escort service in Aerocity now. Call us now!! They love to make relationships for the night with Indian men and that is why they join our escort agency to provide foreign Escorts Service in Mahipalpur. These ladies are referred to as Foreign escorts in Aerocity.
    Escorts Service in Gurgaon
    Body Massage in Vasant Kunj, Delhi
    Body Massage in Gurgaon
    Massage Parlor in Mahipalpur

    ReplyDelete