Check Internet Access on Android

Alessandro
Stackademic
Published in
2 min readFeb 3, 2021

--

A little while ago I had built this Android application that would GET and POST data to a REST API. Simple task, however not everyone has internet access all the time, so then some application logic would have to work around those cases.

A simple third party library would check for current internet access, apply some “clever”… or straight forward logic and make sure data could be pushed to the server when internet access is available. Everything worked fine, until Android 10+… when clients would not be able to POST or GET any data to and from the REST API. After a circus of testing on emulators and different development phones, we could not figure out it if was the Azure server not allowing connections from specific geospatial regions. There was pressure from management and the clients on why it is not always working, well who doesn’t have thrill with the fragmented Android ecosystem ;-).

What do we do now? Well we removed the third party dependency and created a Kotlin object with the following function:

Next we need to add the following to the manifest file:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

Afterwards With this we can check for internet access when we are interacting with the REST API. The full source code for the Android project can be found here: https://github.com/an01f01/internator

Stackademic 🎓

Thank you for reading until the end. Before you go:

--

--

Full mobile stack developer, researcher, simulation software developer, and parent.