0

I have one method that must be execute when device can access to internet(can ping to 8.8.8.8, not just connect or available). What method should I use to detect internet access(can/cannot reach to server) both app running in foreground and background?

I use NetworkCallback(onAvailable() and onLost()) with JobService(by registerNetworkCallback in onStartJob()). It can perform internet connection correctly but not internet access.

class MyNetworkCallback() : ConnectivityManager.NetworkCallback() {

    override fun onAvailable(network: Network?) {
        super.onAvailable(network)
        Log.i("ConnectionStatus", "Connected")
        // perform my method
    }

    override fun onLost(network: Network?) {
        super.onLost(network)
        Log.i("ConnectionStatus", "Disconnected")
    }
}

How to check internet access on Android? InetAddress never times out This topic is good about how to check internet access by many method, but I do not know how to integrate this method to JobService for keep observe internet access to check device can reach to server or not.

What I want:

  1. when app running in both foreground and background, device connect to internet and device can access to server ==> perform method()

  2. when app running in both foreground and background, device connect to internet BUT device cannot access to server ==> NOT perform method()

  3. when app running in both foreground and background, device not connect to internet and device cannot access to server ==> NOT perform method()
  • 1
    Possible duplicate of [Check internet connection in background](https://stackoverflow.com/questions/41828490/check-internet-connection-in-background) – unzila May 15 '19 at 04:38
  • A better way to handle this has already been explained here: [Android event for internet connectivity state change](https://stackoverflow.com/a/25873554/4452048) – Justin Joy May 15 '19 at 04:45
  • both of them just check internet connection state(turn on/off), it not check internet access. What I want is "can access" to internet(can reach to server) not "can connect" – Songpol Rungsawang May 15 '19 at 04:57

0 Answers0