Skip to content

Android kotlin library that provides useful information about the network

License

Notifications You must be signed in to change notification settings

evert-arias/Android-Network-Utility

Repository files navigation

Android Network Utility

Android kotlin library that provides useful information about the network

Getting started

NetworkInfo

Provide information about the device's network connectivity and let you set callbacks function to get notified when certain network related events occurs.

Usage

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)
    }
}

That's all

😀 Enjoy it!

Copyright

MIT © Evert Arias

About

Android kotlin library that provides useful information about the network

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published