Android | Monitor Network Connectivity Using ConnectivityManager.NetworkCallback
ConnectivityManager
class can be used to answer queries about network connectivity status. It can also notifiy applications when network connectivity status changes using ConnectivityManager.NetworkCallback
.
Environment & Tools
Tools used in this post
- Android Studio 3.5
- Google Pixel 3
- Kotlin 1.3.50
ConnectivityManager
ConnectivityManager
class is the starting point to query about networ connectivity status. It can also notifiy applications when network connectivity changes.
An instance of ConnectivityManager
class can give us access to an instance of NetworkInfo
class which can be used to check current active network.
NetworkInfo is deprecated in API 29
NetworkInfo
class holds details about the current active default network. Using NetworkInfo.isConnected()
we can check the network connectivity status before initiating network traffic. However, NetworkInfo
was deprecated in API level 29.
Developers should instead use ConnectivityManager.NetworkCallback
API to monitor connectivity changes.
ConnectivityManager.NetworkCallback
Checking network connectivity and getting notifications about network changes can be achieved by registering ConnectivityManager.NetworkCallback
. Interested applications should extend this class and override the wanted methods such as .onAvailable(Network)
& .onLost(Network)
.
val cm:ConnectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val builder: NetworkRequest.Builder = NetworkRequest.Builder()
cm.registerNetworkCallback(
builder.build(),
object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) {
Log.i("MainActivity", "onAvailable!")
// check if NetworkCapabilities has TRANSPORT_WIFI
val isWifi:Boolean = cm.getNetworkCapabilities(network).hasTransport(
NetworkCapabilities.TRANSPORT_WIFI)
// doSomething()
}
override fun onLost(network: Network) {
Log.i("MainActivity", "onLost!")
// doSomething
}
}
)