Created
November 10, 2016 18:08
-
-
Save LouisCAD/8eb5f659ae7ed7c036b0747da1384fe0 to your computer and use it in GitHub Desktop.
An Agera BaseObservable to monitor connectivity
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
import android.content.BroadcastReceiver | |
import android.content.Context | |
import android.content.Intent | |
import android.content.IntentFilter | |
import android.net.ConnectivityManager.CONNECTIVITY_ACTION | |
import com.google.android.agera.BaseObservable | |
import org.jetbrains.anko.connectivityManager | |
class ConnectivityListener(private val context: Context) : BaseObservable() { | |
val isNetworkConnected: Boolean | |
get() = connectivityManager.activeNetworkInfo?.isConnected ?: false | |
private val connectivityManager = context.connectivityManager | |
private val filter = IntentFilter(CONNECTIVITY_ACTION) | |
private val receiver = object : BroadcastReceiver() { | |
override fun onReceive(context: Context, intent: Intent) = dispatchUpdate() | |
} | |
override fun observableActivated() { | |
context.registerReceiver(receiver, filter) | |
} | |
override fun observableDeactivated() { | |
context.unregisterReceiver(receiver) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment