How to check internet access on Android? InetAddress never times out
Swiftly determinate if your Android device has internet access by executing the ConnectivityManager
for a network connection inspection and confirming real internet connectivity via an HTTP request.
Unfolded code snippet:
This non-UI blocking approach is more than simply checking if you are connected to a network, it actually checks if the internet is truly reachable.
Shaking hands with ConnectivityManager
The downfalls of the often unreliable InetAddress.isReachable
are well documented and it's time to shift towards more dependable options. For efficiently checking connectivity, ConnectivityManager
proves to be a better friend.
The sock(et) to Google DNS trick
One robust method you can implement is by leveraging the Socket
class and attempting a real Internet connection to a reliable DNS server, such as Google's 8.8.8.8
on port 53
. A real connection speaks more volume about internet connectivity than a true
return from isConnected()
.
AsyncTask disambiguation
AsyncTask
being deprecated in API level 30, you're now better off embracing alternatives like Kotlin Coroutines or RxJava that are designed to handle tasks asynchronously more efficiently.
Why InetAddress.isReachable is a false god
While InetAddress.isReachable
may seem like a handy way to check internet access, it often fails due to reasons such as firewalls disallowing ICMP packets and default timeout being too lengthy, leading to a sluggish user experience.
Guidelines for handling the unexpected
The landscape of network conditions can be diverse and unpredictable. Here's how you can gear up for varying connectivity scenarios:
- Multiple network types: Diversify your tests to include
ConnectivityManager.TYPE_MOBILE/WIFI
for a comprehensive internet connectivity check. - Zero signal zones: Take care of a scenario where there's a connection but no internet access, by cross-verifying via HTTP requests to dependable endpoints like Google's
generate_204
marketing endpoint. - Handling UI freeze: Always keep your eye on the main UI thread and avoid heavy operations that could lead it to become unresponsive. This especially holds true when dealing with socket connections.
Oozing future readiness
Keeping up-to-date with current technology trends and future-proofing your solutions is the name of the game in the world of software development:
- Stay API-wise: Remember, different Android versions may have different API limitations and capabilities, code defensively for a wider audience.
- Library love: Libraries like Volley or OkHttp make networking tasks a breeze providing sophisticated features like auto retries, caching, and timeout failovers.
- Asynchronous allure: Leverage from modern asynchronous programming models to keep your app responsive even during network operations.
Was this article helpful?