https://kotlinlang.org logo
Title
k

Kirill Prybylsky

03/06/2020, 12:44 PM
Hi! Is it okay to check via ConnectivityManager do user have an internet connection?
:google: 1
:yes: 4
:stackoverflow: 1
🇳🇴 2
o

Olenyov Kirill

03/06/2020, 1:01 PM
Actually it depends on what you mean “internet connection”) ConnectivityManager will tell you that you have some network connection. But you may have no real Internet. The most right thing you can do here - make a short ping somewhere, e.g. google dns
k

Kirill Prybylsky

03/06/2020, 1:02 PM
Thanks, Kirill. I thinks this pings or checks are kinda “smells” a bit.
o

Olenyov Kirill

03/06/2020, 1:09 PM
Yeah, may be. But it is the only approach i know if you need to check Internet at the moment.
j

Jeremy

03/06/2020, 3:46 PM
You can be connected to network and have no internet (gated wifi) or server could be down
Doing a ping or intercepting exceptions is only reliable way I know
k

Kirill Prybylsky

03/06/2020, 3:49 PM
@Jeremy Exactly! I faced with this problem right now
j

Jeremy

03/06/2020, 3:51 PM
I think if using ConnectivityManager you can say if no network the user def does not have internet but if its true they might have internet
👍 1
k

Kirill Prybylsky

03/06/2020, 3:52 PM
Not really, u can have internet via WIFI and ConnectivityManager still tells you that u don’t have.
🤔 1
just because a minute ago you really hadn’t
o

Olenyov Kirill

03/06/2020, 4:06 PM
A couple month ago I used this version: https://gist.github.com/agent10/cd34308df7c1b42f83bbb3750e2a8ce9 It checks connection very fast
k

Kirill Prybylsky

03/06/2020, 4:08 PM
Thanks for the solution, I will try to change designer’s mind first and if i can’t will use it. But this code smells.
j

Jeremy

03/06/2020, 4:10 PM
🤔 I generally try to ping the API server which just returns a simple response to ensure network is up, api server is up/working
api server can be up but db down for example so you want to switch into "offline mode"
A one liner just "OK"+http200 or "ERROR"+http5xx
If you have retries make sure to enable backoff.. otherwise an api outage will be interesting