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 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 this library in one of the following three ways,
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"
}
}
}
}
// 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"
}
}
}
}
}
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"
}
}
}
}