Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android internet connectivity check

am trying to check for available internet connection, i find many subjects speaking about this, but i still have a problem when i try to verify for the internet connection, the code am using is the flowing:

public boolean isOnline() {
    ConnectivityManager conMgr =
        (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable() &&    conMgr.getActiveNetworkInfo().isConnected()) {
        return true;
    } else {
        return false; 
    }
}

How can help me please ? PS: Sorry for my bad English.

like image 689
Tunandroid Avatar asked May 13 '26 01:05

Tunandroid


1 Answers

Your method is correct. Also you'll need in your android manifest file:

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

But keep in mind that low signal, server downtime, captive portals, content filters, other network problems can prevent your application from reaching a server. So having an active network interface doesn't guarantee that a particular networked service is available.

like image 105
kapandron Avatar answered May 15 '26 16:05

kapandron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!