Android kotlin library that provides useful information about the network
Provide information about the device's network connectivity and let you set callbacks function to get notified when certain network related events occurs.
This is a simple example of how to use the library to receive a notification upon a connectivity change.
class MainActivity : AppCompatActivity(), NetworkInfo.NetworkInfoListener {
// instance of NetworkInfo
private lateinit var networkInfo: NetworkInfo
// called when network's status changes
override fun networkStatusChange(network: NetworkInfo.Network) {
when(network.status){
NetworkInfo.NetworkStatus.INTERNET -> {
Toast.makeText(this, "ONLINE: ${network.type}", Toast.LENGTH_SHORT).show()
}
NetworkInfo.NetworkStatus.OFFLINE -> {
Toast.makeText(this, "OFFLINE: ${network.type}", Toast.LENGTH_SHORT).show()
}
}
}
// onCreate
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// get a singleton instance of the class
networkInfo = NetworkInfo.getInstance(this)
// add a listener
networkInfo.addListener(this)
}
}
😀 Enjoy it!