In every application, it is now mandatory to have Internet connectivity either to serve information from server or sending information to server. But it is very much important to check whether network connection is available or not before retrieving server.
To do so, we have only 3 lines of code which give status of current connectivity.
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val activeNetwork: NetworkInfo? = connectivityManager.activeNetworkInfo
val isConnected: Boolean = activeNetwork?.isConnectedOrConnecting == true
First, we require a context and from there we can take System service of “CONNECTIVITY_SERVICE”.
Then we will fetch network information from that object and finally will check if Internet is available or not. Use this boolean object
isConnected in your condition and perform necessary action if connection available.
It’s very easy!!