🌐 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.
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>")
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
}
}
}
}
Checked internet connection via wifi and mobile network. Download demo
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
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.