Giter VIP home page Giter VIP logo

networklistener's Introduction

NetworkListener

NetworkLister is the library to check the internet connection status of device at runtime.

New Features!

  • Now detect runtime internet speed status

Installation

Project level gradle

allprojects {
 repositories {
    maven { url 'https://jitpack.io' }
 }
}

Module level gradle

implementation 'com.github.AshwinN796:networkListener:1.1.1'
Implementation

Application class :

class MyApplication : Application() {

       override fun onCreate() {
           super.onCreate()
           //init NetworkConfig
           NetworkConfig.initNetworkConfig(this)
       }

       override fun onLowMemory() {
           super.onLowMemory()
           //Remove all listeners while on low memory
           NetworkConfig.getInstance().removeAllNetworkConnectivityListener()

       }
   }

Activity class :

class MainActivity : AppCompatActivity(), NetworkStateListener {

    private var networkConfig : NetworkConfig? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //Get instance of networkConfig class
        networkConfig = NetworkConfig.getInstance()

        //add connectivity listener
        networkConfig!!.addNetworkConnectivityListener(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        //remove connectivity listener
        networkConfig!!.removeNetworkConnectivityListener(this)
    }

    /*
        Do action on network status changed
        Here you can perform any action for Network state listener depending on your requirement.
     */
    override fun onNetworkStatusChanged(isConnected: Boolean) {

//        when(isConnected){
//            true -> {//make your action}
//            false -> {}
//        }
    }

    override fun onNetworkSpeedChanged(speedType: Int) {
        when(speedType) {
            NetworkConstant.WIFI_CONNECTED -> {//make your action}
            NetworkConstant.FULL_SPEED_CONNECTED -> {}
            NetworkConstant.SLOW_CONNECTED ->  {}
            NetworkConstant.LOW_SPEED_CONNECTED -> {}
        }
    }
}

networklistener's People

Contributors

ashwinn796 avatar ashwinmobi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.