ConnectivityListener is a simple library that notifies you about connectivity changes !
ConnectivityListener enable you to :
- Get current connectivity's state
- Listen for connectivity changes
We wanted to make ConnectivityListener as simple as possible to use, so we created 2 libraries in order to make you use the more suitable for your use case.
This is the most basic implementation. It let the user responsible to register
and unregister
the listener
Steps are :
implementation("com.smartnsoft:connectivitylistener:1.0-SNAPSHOT")
1.Register the Listener in an active
LifeCycle :
connectivityListener = ConnectivityListener(this)
connectivityListener.register()
2.Set the listener :
connectivityListener.setListener(this)
3.Do something with the informations about connectivity :
override fun onConnectivityInformationChanged(connectivityInformation: ConnectivityInformation)
{
runOnUiThread {
when (connectivityInformation)
{
ConnectivityInformation.Wifi -> // WIFI CONNECTED
ConnectivityInformation.Mobile -> // Mobile CONNECTED
else -> // Mobile CONNECTED
}
}
}
4.Unregister the listener when your app goes in an inactive
Lifecycle :
connectivityListener.unregister();
In parallel, you can request the current connectivity information at any time :
when (connectivityListener.getConnectionInformation())
{
ConnectivityInformation.Wifi -> // WIFI CONNECTED
ConnectivityInformation.Mobile -> // Mobile CONNECTED
else -> // Mobile CONNECTED
}
This library use the power of LiveData and the fact that they are LifeCycle aware in order to prevent the user from having to register and unregister the listener.
Steps are :
implementation("com.smartnsoft:livedataconnectivitylistener:1.0-SNAPSHOT")
1.Observe LiveData changes :
LiveDataConnectivityListener(this).observe(this, Observer<ConnectivityInformation> { connectionModel ->
when (connectionModel)
{
ConnectivityInformation.Wifi -> // WIFI CONNECTED
ConnectivityInformation.Mobile -> // Mobile CONNECTED
else -> // NO internet
}
})
In parallel, you can request the current connectivity information at any time :
when (connectivityListener.getConnectionInformation())
{
ConnectivityInformation.Wifi -> // WIFI CONNECTED
ConnectivityInformation.Mobile -> // Mobile CONNECTED
else -> // Mobile CONNECTED
}
Please refer to app
module to see 2 implementations of the library corresponding to Basic and LiveData libraries.