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);
}
No comments:
Post a Comment