0

I've already read many other answers posted on the internet but they are not exactly what I'm looking for, I know how to check if the device is connected/disconnected to the internet/mobile data what I am trying to do is check if data is transferable through that network or not, you might be connected to a network but might not be able to transfer data through that network.

here is what I'm doing now

 val listener = object : ConnectivityManager.NetworkCallback() {
        override fun onAvailable(network: Network) {
            super.onAvailable(network)
            isActiveNetwork()
        }

        override fun onUnavailable() {
            super.onUnavailable()
       }

        override fun onLost(network: Network) {
            super.onLost(network)
            // no internet
         }
    }

(getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager).registerNetworkCallback(NetworkRequest.Builder().build(), listener )

the isActiveNetwork method is used to check if the internet connection is active or not

private fun isActiveNetwork(){
    try {
        if(InetAddress.getByName("www.google.com").isReachable(5000)){
             //active
        }            
        else{
            // not active
        } 
    } catch (e: Exception) {
        //not active
    }
}

the problem is this method will check if the internet connection is active once and that's all, I've tried to do this

private fun isActiveNetwork(){
    try {
        if(InetAddress.getByName("www.google.com").isReachable(5000)){
             //active
        }            
        else{
            isActiveNetwork()
        } 
    } catch (e: Exception) {
        isActiveNetwork()
    }
}

to try and keep checking every 5 seconds to see if the internet connection is active, but that caused my app to crash because of StackOverflow.... any suggestions?

Krishna Sony
  • 1,286
  • 13
  • 27
MoTahir
  • 863
  • 7
  • 22
  • Please have a look at [this](https://stackoverflow.com/questions/32547006/connectivitymanager-getnetworkinfoint-deprecated/54641263#54641263) answer, it's in java but will give some clue – Zain Jun 05 '20 at 16:54

1 Answers1

1

You can use Runnable and post it using handler to continuously check internet connection every 5 second. Declare a global boolean variable isConnected and create a Runnable like:

private var isActiveNetwork= object : Runnable {
        override fun run() {
            try {
                if(InetAddress.getByName("www.google.com").isReachable(5000)) 
                 {
                  isConnected = true
                 }            
               else{
                  isConnected = false
                 }
               Handler().postDelayed(this,5000) 
              } 
          catch (e: Exception) {
              isConnected = false
         }

        }
    }

Now you can start this Runnable from network request callback:

 override fun onAvailable(network: Network) {
            super.onAvailable(network)
            Handler().post(isActiveNetwork)
        }
MoTahir
  • 863
  • 7
  • 22
Alpha 1
  • 4,118
  • 2
  • 17
  • 23
  • this kind of works but you need to run it on background thread not main thread – MoTahir Jun 05 '20 at 18:08
  • @MoTahir won't main thread blocked when checked continuously without delaying? – Animesh Sahu Jun 06 '20 at 02:49
  • 1
    If you apply the code above inside an activity, it won't be affected somehow, I actually got the main thread error when I was using the main looper (in the activity), but if you make a separate file for the network Listener then use, you need to use the main looper – MoTahir Jun 07 '20 at 11:27