
Lightweight utility class enables real-time network connectivity monitoring, handles network type changes, and supports callback mechanisms for tracking WiFi, cellular, and Ethernet connections.
Android Network Watcher is a lightweight Android utility class that provides easy-to-use network connectivity monitoring. It helps developers track network connection changes and determine the current network type with minimal setup.
Add the ConnectivityMonitor.kt file to your Android project's source directory.
Add the following dependency to your build.gradle.kts file:
implementation("io.github.kdroidfilter:netwatcher:0.1.0")class YourActivity : AppCompatActivity(), ConnectivityMonitor.ConnectivityListener {
private lateinit var connectivityMonitor: ConnectivityMonitor
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
connectivityMonitor = ConnectivityMonitor(this, this)
connectivityMonitor.startMonitoring()
}
// Implement the connectivity listener
override fun onNetworkConnectionChanged(isConnected: Boolean, networkType: ConnectivityMonitor.NetworkType) {
when {
isConnected -> {
when (networkType) {
ConnectivityMonitor.NetworkType.WIFI ->
println("Connected to WiFi")
ConnectivityMonitor.NetworkType.CELLULAR ->
println("Connected to Cellular Network")
ConnectivityMonitor.NetworkType.ETHERNET ->
println("Connected to Ethernet")
else -> println("Connected to Unknown Network")
}
}
else -> println("No network connection")
}
}
override fun onDestroy() {
connectivityMonitor.stopMonitoring()
super.onDestroy()
}
}Begins monitoring network connectivity changes. Immediately triggers an initial callback with the current network state.
Stops monitoring network connectivity and unregisters the network callback.
The library supports the following network types:
WIFI: WiFi network connectionCELLULAR: Mobile data networkETHERNET: Ethernet networkNONE: No network connectionUNKNOWN: Unrecognized network typeThis project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
Android Network Watcher is a lightweight Android utility class that provides easy-to-use network connectivity monitoring. It helps developers track network connection changes and determine the current network type with minimal setup.
Add the ConnectivityMonitor.kt file to your Android project's source directory.
Add the following dependency to your build.gradle.kts file:
implementation("io.github.kdroidfilter:netwatcher:0.1.0")class YourActivity : AppCompatActivity(), ConnectivityMonitor.ConnectivityListener {
private lateinit var connectivityMonitor: ConnectivityMonitor
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
connectivityMonitor = ConnectivityMonitor(this, this)
connectivityMonitor.startMonitoring()
}
// Implement the connectivity listener
override fun onNetworkConnectionChanged(isConnected: Boolean, networkType: ConnectivityMonitor.NetworkType) {
when {
isConnected -> {
when (networkType) {
ConnectivityMonitor.NetworkType.WIFI ->
println("Connected to WiFi")
ConnectivityMonitor.NetworkType.CELLULAR ->
println("Connected to Cellular Network")
ConnectivityMonitor.NetworkType.ETHERNET ->
println("Connected to Ethernet")
else -> println("Connected to Unknown Network")
}
}
else -> println("No network connection")
}
}
override fun onDestroy() {
connectivityMonitor.stopMonitoring()
super.onDestroy()
}
}Begins monitoring network connectivity changes. Immediately triggers an initial callback with the current network state.
Stops monitoring network connectivity and unregisters the network callback.
The library supports the following network types:
WIFI: WiFi network connectionCELLULAR: Mobile data networkETHERNET: Ethernet networkNONE: No network connectionUNKNOWN: Unrecognized network typeThis project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.