Saturday, October 2, 2021

Check Internet Connection Type In Android

 In Android we can check whether the Device is Connected to Internet or Not.


If Connected to Internet, we can check the type of connection like Mobile Data, WiFi etc


Add following permission in Manifest file

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Code to Check Internet Connection Type in Android


public boolean isInternetConnected() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
boolean isConncted = activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting();
return isConncted;
}


public boolean isConnectedWifi(){
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI);
}



public boolean isConnectedMobile(){
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_MOBILE);
}

1 comment:

  1. Wishing you the best of luck for all your blogging efforts. My site: okbet login

    ReplyDelete