Skip to content

Instantly share code, notes, and snippets.

@aligkts
Created November 18, 2020 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aligkts/4a3fc06c18c56ed37249d0ac6c97d0e2 to your computer and use it in GitHub Desktop.
Save aligkts/4a3fc06c18c56ed37249d0ac6c97d0e2 to your computer and use it in GitHub Desktop.
ConnectivityReceiver
class ConnectivityReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, arg1: Intent) {
if (connectivityReceiverListener != null) {
connectivityReceiverListener!!.onNetworkConnectionChanged(isConnectedOrConnecting(context))
}
}
private fun isConnectedOrConnecting(context: Context): Boolean {
val connectedManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val networkInfo = connectedManager.activeNetworkInfo
return networkInfo != null && networkInfo.isConnectedOrConnecting
}
interface ConnectivityReceiverListener {
fun onNetworkConnectionChanged(isConnected: Boolean)
}
companion object {
var connectivityReceiverListener: ConnectivityReceiverListener? = null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment