The ConnectivityLiveData library provides a simple way to monitor network connectivity status changes in an Android application using LiveData. It is available on Jitpack and can be added to an Android project by following the instructions provided below.
Add this in your root settings.gradle
:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
// Add this jitpack URL
maven { url 'https://jitpack.io' }
}
}
This library is available on Jitpack
Add this to your module's build.gradle
file (make sure the Tag matches the Jitpack version badge above):
dependencies {
...
implementation 'com.github.Micoder-dev:ConnectivityLiveData:Tag'
}
To implement the ConnectivityLiveData:
class MainActivity : AppCompatActivity() {
// Declare a property for LiveData that tracks network connectivity
private lateinit var connectivityLiveData: ConnectivityLiveData
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Call the function to check network connectivity
checkConnection()
}
// Function to check network connectivity using LiveData
private fun checkConnection() {
// Initialize the LiveData property with the application context
connectivityLiveData = ConnectivityLiveData(application)
// Observe changes to the network connectivity status
connectivityLiveData.observe(this) { isAvailable ->
// Check if network connectivity is available or not
when(isAvailable) {
true -> (Has Network Connection) // Do Something
false -> (No Network Connection) // Do Something
}
}
}
}
Handling Network Connectivity using Lottie Animation Dialog
Network Not Available |
Network Available |
Please fork this repository and contribute back using pull requests.
Any contributions, large or small, major features, bug fixes, are welcomed and appreciated but will be thoroughly reviewed .
Don't forget to star ⭐ the repo it motivates me to share more open source
Copyright 2023 Mugunthan
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.