Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check Network and Internet Connection - Android

I was wondering if the method below check that I am both connected to a network, and can actually connected to the internet as well.

Not just connected to a network that won't let me access the internet ?

public boolean isNetworkAvailable() {
    ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = manager.getActiveNetworkInfo();

    boolean isAvailable = false;
    if (networkInfo != null && networkInfo.isConnected()) {
        isAvailable = true;
    }
    return isAvailable;
}

I think, it does but I am not 100% sure.

Thanks

like image 301
spen123 Avatar asked Jul 08 '15 04:07

spen123


2 Answers

On comparing the accepted answer on this post to your code, what you are doing should work. Feel free to compare code. The safest thing to do would be to run a few tests from airplane mode, with the WiFi turned off, and from a location away from WiFi just to be sure. Good luck.

Android - Programmatically check internet connection and display dialog if notConnected

like image 185
wolfaviators Avatar answered Oct 03 '22 11:10

wolfaviators


Have a look into one of my old answers. It has two different methods 1. to check if a device is connected to a network 2. to check if a device is connected to Internet.

like image 36
Seshu Vinay Avatar answered Oct 03 '22 09:10

Seshu Vinay