0

I am using the getActiveNetworkInfo to get the network that the user is connected and using the isConnected to verify if it is connected. I am doing this with the following code:

val connectivityManager = getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager?
val activeNetworkInfo = connectivityManager?.activeNetworkInfo
return activeNetworkInfo?.isConnected == true

The problem is that if the user is connected for example to a wifi that has no internet connection, the isConnected returns true.

How can I verify if this network connection has internet connection?

I know that I can execute a request to some service to verify if the user is connected, but my intention is to show a banner for the user inside the app to notify him that he has no internet connection. So for example when he request something, I can notify the user that it will not work and don't show a loading and then an error message.

Keep pooling a service to verify the internet connection is not an alternative either way, it will not be efficient in terms of data and battery usage.

jonathanrz
  • 4,206
  • 6
  • 35
  • 58
  • @ישו אוהב אותך this is not a duplicate. Did you read my question? I placed the code of the answer you marked as duplicate and explained why this is not enough for what I want. – jonathanrz Jun 13 '19 at 15:27

1 Answers1

1

I don't think Android supports checking if the network being connected to is really usable. The only way is to ping some site yourself, or just show the error message banner after the user requesting a connection.

TylerQITX
  • 318
  • 2
  • 9
  • when you are connected to a wifi that doesn't have connection, Android shows a message in the wifi level, so internally this info is available. So this maybe is just about creating an api to make this info available for the developers. – jonathanrz Jun 13 '19 at 15:29