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:
when app running in both foreground and background, device connect to internet and device can access to server ==> perform method()
when app running in both foreground and background, device connect to internet BUT device cannot access to server ==> NOT perform method()
- when app running in both foreground and background, device not connect to internet and device cannot access to server ==> NOT perform method()