Skip to content Skip to sidebar Skip to footer

How To Check If Internet Is Active With Wifi Network Connected In Android

How to check automatically if internet is active on the WiFi network connected in android? I can check if wifi is enabled or if wifi network is connected but I am not sure how to c

Solution 1:

publicbooleanisOnline() {
    ConnectivityManagercm=
        (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfonetInfo= cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        returntrue;
    }
    returnfalse;
}

Solution 2:

If you want to check your device is connected with wifi then use this method.

publicstaticbooleanisInternetConnected(Context context) {
    ConnectivityManagerconnManager= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfomnetwork= connManager.getActiveNetworkInfo();
    return mnetwork != null && mnetwork.isConnected();
}

And if you want to check your device is connected with Mobile Network Internet then use this method.

publicstaticbooleanisMobileInternetConnected(Context context) {
        ConnectivityManagerconnManager= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfomNetwork= connManager.getActiveNetworkInfo();
        return mNetwork != null && mNetwork.isConnected() && mNetwork.getType() == ConnectivityManager.TYPE_MOBILE;
    }

Note: Don't Forget to Add Permission

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

Post a Comment for "How To Check If Internet Is Active With Wifi Network Connected In Android"