Skip to content

Latest commit

 

History

History
183 lines (150 loc) · 5.03 KB

README.md

File metadata and controls

183 lines (150 loc) · 5.03 KB

ConnectionChecker

Release

Android library for checking the internet connectivity of a device.

Used in https://play.google.com/store/apps/details?id=com.muddassirkhan.quran_android

Add Dependencies

Add the following in your project level build.gradle

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

and the following in your app level build.gradle

dependencies {
    implementation 'com.github.muddassir235:connection_checker:1.7'
}

Use The Library

Use this library in one of the following three ways,

1. Using a method and an interface

checkConnection(this) // this: lifecycleOwner (e.g. Activity) which has implemented ConnectivityListener

By default it will ping https://www.google.com. The user can set the url to ping.

checkConnection(this, "https://www.site.url")

By default the least required lifecycle state is Lifecycle.State.RESUMED. The user can set it to what they require.

checkConnection(this, "https://www.site.url", Lifecycle.State.STARTED)

Example in an Android Activity.

class MainActivity : AppCompatActivity(), ConnectivityListener {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        checkConnection(this)
    }

    override fun onConnectionState(state: ConnectionState) {
        connection_status_tv.text = when (state) {
            ConnectionState.CONNECTED -> {
                "Connected"
            }
            ConnectionState.SLOW -> {
                "Slow Internet Connection"
            }
            else -> {
                "Disconnected"
            }
        }
    }
}

2. [Or] Using a method and a lambda callback

// this is a lifecycleOwner (e.g. Activity or ViewLifecycleOwner)
checkConnection(this) { connectionState ->
    // Your logic here
}

By default it will ping https://www.google.com. The user can set the url to ping.

checkConnection(this, "https://www.site.url") { connectionState ->
    // Your logic here
}

By default the least required lifecycle state is Lifecycle.State.RESUMED. The user can set it to what they require.

checkConnection(this, "https://www.site.url", Lifecycle.State.STARTED) { connectionState ->
    // Your logic here
}

Example in an Android Activity.

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

        checkConnection(this) { connectionState ->
            connection_status_tv.text = when(connectionState) {
                ConnectionState.CONNECTED -> {
                    "Connected"
                }
                ConnectionState.SLOW -> {
                    "Slow Internet Connection"
                }
                else -> {
                    "Disconnected"
                }
            }
        }
    }
}

3. [Or] Using a class object and an interface

val connectionChecker = ConnectionChecker(this)

By default it will ping https://www.google.com. The user can set the url to ping.

val connectionChecker = ConnectionChecker(this, "https://www.site.url")

By default the least required lifecycle state is Lifecycle.State.RESUMED. The user can set it to what they require.

val connectionChecker = ConnectionChecker(this, "https://www.site.url", Lifecycle.State.STARTED)

Add connectivity listener

connectionChecker.connectivityListener = object: ConnectivityListener {
    override fun onConnectionState(state: ConnectionState) {
        // Your logic goes here
    }
}

Example in an Android Activity.

class MainActivity : AppCompatActivity(), ConnectivityListener {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val connectionChecker = ConnectionChecker(this, lifecycle)
        connectionChecker.connectivityListener = this
    }

    override fun onConnectionState(state: ConnectionState) {
        connection_status_tv.text = when (state) {
            ConnectionState.CONNECTED -> {
                "Connected"
            }
            ConnectionState.SLOW -> {
                "Slow Internet Connection"
            }
            else -> {
                "Disconnected"
            }
        }
    }
}

Uses

Muddassir Ahmed Links: