6

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

Anoop M Maddasseri
  • 10,213
  • 3
  • 52
  • 73
spen123
  • 3,464
  • 11
  • 39
  • 52
  • `networkInfo.isConnected()` checks whether network connectivity exists and if it is possible to establish connections and pass data. You can also use `networkInfo.isConnectedOrConnecting()` to check whether network connectivity exists or is in the process of being established. Also, make sure you add permissions to your AndroidManifest.xml to access wifi and network state, respectively: `` `` – Virat Singh Jul 08 '15 at 04:50
  • Possible duplicate of [How to check internet access on Android? InetAddress never times out](https://stackoverflow.com/questions/1560788/how-to-check-internet-access-on-android-inetaddress-never-times-out) – Maciej Treder Feb 07 '18 at 10:57

4 Answers4

2

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

Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84
wolfaviators
  • 503
  • 1
  • 7
  • 21
2

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.

Community
  • 1
  • 1
Seshu Vinay
  • 13,560
  • 9
  • 60
  • 109
0

The code below will check the internet connectivity using kotlin in android studio:

private fun amIConnected(): Boolean {
    val connectivityManager = this.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
    val activeNetworkInfo = connectivityManager.activeNetworkInfo
    return activeNetworkInfo != null && activeNetworkInfo.isConnected
}
JonathanDavidArndt
  • 2,518
  • 13
  • 37
  • 49
0

I tried the approach of user:3156908 but it kept crashing my app when the internet connection was off. Turns out there was a logical error on his code so i used the connectivityManager.getActiveNetworkInfo() != null to check for network details. In case the device is connected to the network or it is in the process of connecting to the network use the isConnectedOrConnecting()

In your AndroidManifest.xml file add the following

<!--  Internet Permissions -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Create a method called checkInternetConnection in your fragment class or Activity class and add the following lines of code.

 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private boolean checkInternetConnection(){
        ConnectivityManager connectivityManager = (ConnectivityManager) mcontext.getSystemService(mcontext.CONNECTIVITY_SERVICE);
        return connectivityManager.getActiveNetworkInfo() != null
                && connectivityManager.getActiveNetworkInfo().isConnectedOrConnecting();
    }

Then you can call the checkInternetConnection in your fragment's onViewCreated method or your activity's onCreate method.

Note I would strongly advise you to test it the code when its connected to wifi, mobile data and airplane mode.

Nelson Katale
  • 1,309
  • 15
  • 21