NetworkX
An easy & handy library to monitor device internet connection status.
Documentation
Installation
Step 1. Add the JitPack repository to your build file .
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
Step 2. Add the dependency.
dependencies {
implementation 'com.github.rommansabbir:NetworkX:Tag'
}
Version available
Releases |
---|
3.3.0 |
Usages
Step 1:
Initialize NetworkX
from your Application.onCreate()
val builder = NetworkXConfig.Builder()
.withApplication(this)
// You can disable speed meter if not required
.withEnableSpeedMeter(true)
.build()
NetworkXProvider.enable(builder)
Step 2:
- To check internet connection status, simply call
NetworkXProvider.isInternetConnected
which return aBoolean
value.
val status = NetworkXProvider.isInternetConnected
textView.text = "Internet connection status: $status"
- To observe the internet connection status, start observing
NetworkXProvider.isInternetConnectedLiveData
NetworkXProvider.isInternetConnectedLiveData.observe(this) { status ->
status?.let {
textView.text = "Internet connection status: $it"
}
}
- To get connected network speed/last known speed call
NetworkXProvider.lastKnownSpeed
which return anLastKnownSpeed
object
NetworkXProvider.lastKnownSpeed?.let {
textView2.text =
"Last Known Speed: Speed - ${it.speed} | Type - ${it.networkTypeNetwork} | Simplified Speed - ${it.simplifiedSpeed}"
}
- To obsever current network speed/last known speed start observing
NetworkXProvider.lastKnownSpeedLiveData
NetworkXProvider.lastKnownSpeedLiveData.observe(this) {
it?.let {
textView2.text =
"Last Known Speed: Speed - ${it.speed} | Type - ${it.networkTypeNetwork} | Simplified Speed - ${it.simplifiedSpeed}"
}
}
Notes:
- NetworkX (including Speed Meter) only works when the app is in the foreground
- The default value for
NetworkXProvider.isInternetConnected
isfalse
- The default value for
NetworkXProvider.isInternetConnectedLiveData
isnull
- The default value for
NetworkXProvider.lastKnownSpeed
isnull
- The default value for
NetworkXProvider.lastKnownSpeedLiveData
isnull
How to show the NoInternetDialog
?
NoInternetDialog
.Companion
.Builder()
// Provide activity reference
.withActivity(this)
// Provide custom title
.withTitle("No internet!")
// Provide custom mesage
.withMessage("Your device is not connected to the internet!")
// Register for callback
.withActionCallback {
// User clicked `Retry` button
}
.build()
.show()
- Also, you can determine if the
NoInternetDialog
is currently visible or not by calling this methodNoInternetDialog.isDialogVisible()
which return anBoolean
.
Happy Coding....
Contact me
License
Copyright (C) 2021 Romman Sabbir
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.