Last active
March 29, 2021 12:33
-
-
Save marenovakovic/2b3d4bdbaead94a901cbe73ddb28a93d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NetworkStatusTracker(context: Context) { | |
private val connectivityManager = | |
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | |
init { | |
val networkStatusCallback = object : ConnectivityManager.NetworkCallback() { | |
override fun onUnavailable() = println("onUnavailable") | |
override fun onAvailable(network: Network) = println("onAvailable") | |
override fun onLost(network: Network) = println("onLost") | |
} | |
val request = NetworkRequest.Builder() | |
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) | |
.build() | |
connectivityManager.registerNetworkCallback(request, networkStatusCallback) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment