Skip to content

Instantly share code, notes, and snippets.

@chydee
Created December 25, 2020 01:20
Show Gist options
  • Save chydee/7e7384bcbf0c0060dcc7c8057f8b0597 to your computer and use it in GitHub Desktop.
Save chydee/7e7384bcbf0c0060dcc7c8057f8b0597 to your computer and use it in GitHub Desktop.
Check And Report Internet Connctivity In RealTime
val networkMonitor = NetworkMonitorUtil(requireContext())
override fun onStart() {
super.onStart()
networkMonitor.register()
}
override fun onStop() {
super.onStop()
networkMonitor.unregister()
}
private fun reportNetworkState() {
networkMonitor.result = { isAvailable, type ->
requireActivity().runOnUiThread {
when (isAvailable) {
true -> {
when (type) {
ConnectionType.Wifi -> {
Timber.i("Wifi Connection")
//Do Something here
}
ConnectionType.Cellular -> {
Timber.i("Cellular Connection")
//Do Somehting here
}
else -> {
//DO Something
}
}
}
false -> {
Timber.i("No Connection")
///Do Something here
}
}
}
}
}
enum class ConnectionType {
Wifi, Cellular
}
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkCapabilities
import android.os.Build
@Suppress("DEPRECATION")
class NetworkMonitorUtil(context: Context) {
private var mContext = context
private lateinit var networkCallback: ConnectivityManager.NetworkCallback
lateinit var result: ((isAvailable: Boolean, type: ConnectionType?) -> Unit)
fun register() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
//Use NetworkCallback for Android 9 and above
val connectivityManager =
mContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
if (connectivityManager.activeNetwork == null) {
//UNAVAILABLE
result(false, null)
}
//Check when the connection changes
networkCallback = object : ConnectivityManager.NetworkCallback() {
override fun onLost(network: Network) {
super.onLost(network)
//UNAVAILABLE
result(false, null)
}
override fun onCapabilitiesChanged(
network: Network,
networkCapabilities: NetworkCapabilities
) {
super.onCapabilitiesChanged(network, networkCapabilities)
when {
networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> {
//WIFI
result(true, ConnectionType.Wifi)
}
else -> {
result(true, ConnectionType.Cellular)
}
}
}
}
connectivityManager.registerDefaultNetworkCallback(networkCallback)
} else {
//Use Intent Filter for Android 8 and below
val intentFilter = IntentFilter()
intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE")
mContext.registerReceiver(networkChangeReceiver, intentFilter)
}
}
fun unregister() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
val connectivityManager =
mContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
connectivityManager.unregisterNetworkCallback(networkCallback)
} else {
mContext.unregisterReceiver(networkChangeReceiver)
}
}
//Monitor network change and pass value to result
private val networkChangeReceiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val connectivityManager =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val activeNetworkInfo = connectivityManager.activeNetworkInfo
if (activeNetworkInfo != null) {
// Get Type of Connection
when (activeNetworkInfo.type) {
ConnectivityManager.TYPE_WIFI -> {
//WIFI
result(true, ConnectionType.Wifi)
}
else -> {
//CELLULAR
result(true, ConnectionType.Cellular)
}
}
} else {
//UNAVAILABLE
result(false, null)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment