Hi! Is it okay to check via ConnectivityManager do...
# android
k
Hi! Is it okay to check via ConnectivityManager do user have an internet connection?
google 1
đź‘Ś 4
stackoverflow 1
đźš« 2
o
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
Thanks, Kirill. I thinks this pings or checks are kinda “smells” a bit.
o
Yeah, may be. But it is the only approach i know if you need to check Internet at the moment.
j
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
@Jeremy Exactly! I faced with this problem right now
j
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
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
A couple month ago I used this version: https://gist.github.com/agent10/cd34308df7c1b42f83bbb3750e2a8ce9 It checks connection very fast
k
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
🤔 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