Skip to content

๐ŸŒ Light library to check or observe the real internet connection in android apps easily

License

Notifications You must be signed in to change notification settings

raheemadamboev/check-internet-android

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

43 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Check Internet

Android Version License API

๐ŸŒ CheckInternetAndroid is a light library to check or observe internet connection in Android apps easily. It checks the real internet connection by connecting to the Google's DNS server. If call is successful, then the internet is working, else it is not working.

Setup

Add the maven library bucket to the dependencyResolutionManagement.repositories block in settings.gradle.kts file as follows:

dependencyResolutionManagement {
  ...
  repositories {
    ...
    maven("https://jitpack.io")
  }
}

Install the library to the project in desired module's build.gradle.kts file. Replace <current_version> with the actual version:

implementation("com.github.raheemadamboev:check-internet-android:<current_version>")

Implementation

Check internet connection (callback API):

CheckInternet(applicationContext).check { connected ->
  if (connected) { 
      // there is internet                
  } else { 
      // there is no internet                  
  }
}

Check internet connection (suspend API):

viewModelScope.launch {
  val connected = CheckInternet(applicationContext).check()
  if (connected) { 
      // there is internet                
  } else { 
      // there is no internet                  
  }
}

Observe internet status (StateFlow)

private val internet: CheckInternet by lazy { CheckInternet(applicationContext) }

override fun onResume() {
  super.onResume()
  internet.startObservingConnection()
}

override fun onPause() {
  super.onPause()
  internet.stopObservingConnection()
}

lifecycleScope.launch {
  internet.status.collectLatest { status ->
    when(status) {
      Initial -> {
        // initial state, internet is not being observed, should call startObservingConnection()
      }

      Connected -> {
        // there is internet
      }

      NotConnected -> {
        // there is no internet
      }
    }
  }
}

Demo

Checked internet connection via wifi and mobile network. Download demo

Projects using this library

GoTest 250 000+ downloads in Google Play Store

Buxgalteriya schyotlar rejasi 50 000+ downloads in Google Play Store

Irregular Verbs 25 000+ downloads in Google Play Store

License

Designed and developed by raheemadamboev (Raheem) 2022.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

๐ŸŒ Light library to check or observe the real internet connection in android apps easily

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages