Android library for checking the internet connectivity of a device.

Overview

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.

{ "Slow Internet Connection" } else -> { "Disconnected" } } } } ">
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.

// Your logic here } ">
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.

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

Example in an Android Activity.

{ "Slow Internet Connection" } else -> { "Disconnected" } } } } } ">
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.

{ "Slow Internet Connection" } else -> { "Disconnected" } } } } ">
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

Apps by Muddassir Ahmed:

Muddassir Ahmed Links:

You might also like...
UPnP/DLNA library for Java and Android

Cling EOL: This project is no longer actively maintained, code may be outdated. If you are interested in maintaining and developing this project, comm

Android library for viewing, editing and sharing in app databases.
Android library for viewing, editing and sharing in app databases.

DbInspector DbInspector provides a simple way to view the contents of the in-app database for debugging purposes. There is no need to pull the databas

Android Market In-app Billing Library

Update In-app Billing v2 API is deprecated and will be shut down in January 2015. This library was developed for v2 a long time ago. If your app is st

Android library to easily serialize and cache your objects to disk using key/value pairs.

Deprecated This project is no longer maintained. No new issues or pull requests will be accepted. You can still use the source or fork the project to

Form Validator Library for Android

Android-Validator Form Validator Library for Android [](https://flattr.com/submit/auto?user_id=throrin19&url=https://github.com/throrin19/Android-Vali

Very easy to use wrapper library for Android SharePreferences

Treasure English document Treasure是一个Android平台上基于SharePreferences的偏好存储库,只需要定义接口,无需编写实现,默认支持Serializable和Parcelable。运行时0反射,不仅使用方便而且性能和原生写法几乎无差别。 使用方法 1

Error handling library for Android and Java

ErrorHandler Error handling library for Android and Java Encapsulate error handling logic into objects that adhere to configurable defaults. Then pass

Small Android library to help you incorporate MVP, Passive View and Presentation Model patterns in your app
Small Android library to help you incorporate MVP, Passive View and Presentation Model patterns in your app

DroidMVP About DroidMVP is a small Android library to help you incorporate the MVP pattern along with Passive View and Presentation Model (yes, those

A simple Android utils library to write any type of data into cache files and read them later.

CacheUtilsLibrary This is a simple Android utils library to write any type of data into cache files and then read them later, using Gson to serialize

Comments
  • java.lang.OutOfMemoryError: pthread_create

    java.lang.OutOfMemoryError: pthread_create

    I got this error

    java.lang.OutOfMemoryError: pthread_create (1040KB stack) failed: Try again at java.lang.Thread.nativeCreate(Native Method) at java.lang.Thread.start(Thread.java:883) at com.android.volley.RequestQueue.start(RequestQueue.java:134) at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:91) at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:67) at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:102) at com.muddassir.connection_checker.MacrosKt.pingUrl(macros.kt:12) at com.muddassir.connection_checker.ConnectionChecker.evaluateConnection(ConnectionChecker.kt:58) at com.muddassir.connection_checker.ConnectionChecker.check(ConnectionChecker.kt:43) at com.muddassir.connection_checker.ConnectionChecker.access$check(ConnectionChecker.kt:29) at com.muddassir.connection_checker.ConnectionChecker$checkAgain$$inlined$delay$1.run(functions.kt:23)

    opened by abahe 5
Releases(1.7)
Owner
Muddassir Ahmed Khan
Experienced in Application Development, Udacity Certified Machine Learning Engineer. Play Store Apps: (https://bit.ly/394Mwhy, https://bit.ly/338ft8J)
Muddassir Ahmed Khan
An Android library allowing images to exhibit a parallax effect that reacts to the device's tilt

Motion An Android library allowing images to exhibit a parallax effect. By replacing static pictures and backgrounds with a fluid images that reacts t

Nathan VanBenschoten 781 Nov 11, 2022
Use Android as Rubber Ducky against another Android device

Use Android as Rubber Ducky against another Android device

null 1.4k Jan 9, 2023
Android device shake detection.

Seismic Android device shake detection. Download Download the latest .jar or depend via Maven: <dependency> <groupId>com.squareup</groupId> <artif

Square 1.2k Dec 27, 2022
A small utility to record Android device screen to a GIF

RoboGif A small utility to record Android device screen to an optimized GIF so you can paste it to GitHub or a similar service. Requirements Python 2.

Jernej Virag 526 Dec 9, 2022
adds an option to the Android Sharesheet that allows you to save files to your device.

Save On Device adds an option to the Android Sharesheet that allows you to save files to your device. Download Get the app from the Google Play Store

null 24 Nov 29, 2022
Android tiny device flow client

OAuth 2.0 Device Flow Example Setup Create an Auth0 application Enable Device Code Grants in Advanced settings Disable Client Credentials Enable Devic

Kenji Saito 0 May 15, 2022
Android library which makes it easy to handle the different obstacles while calling an API (Web Service) in Android App.

API Calling Flow API Calling Flow is a Android library which can help you to simplify handling different conditions while calling an API (Web Service)

Rohit Surwase 19 Nov 9, 2021
Android Utilities Library build in kotlin Provide user 100 of pre defined method to create advanced native android app.

Android Utilities Library build in kotlin Provide user 100 of pre defined method to create advanced native android app.

Shahid Iqbal 4 Nov 29, 2022
A robust native library loader for Android.

ReLinker A robust native library loader for Android. More information can be found in our blog post Min SDK: 9 JavaDoc Overview The Android PackageMan

Keepsafe 2.9k Dec 27, 2022
Joda-Time library with Android specialization

joda-time-android This library is a version of Joda-Time built with Android in mind. Why Joda-Time? Android has built-in date and time handling - why

Daniel Lew 2.6k Dec 9, 2022