Created
November 18, 2020 12:06
-
-
Save aligkts/4a3fc06c18c56ed37249d0ac6c97d0e2 to your computer and use it in GitHub Desktop.
ConnectivityReceiver
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 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