Skip to content

Repository files navigation

Android Connectivity Observer

Platform Kotlin Min API Compose License

A lightweight Kotlin library for observing network connectivity on Android.

Detect WiFi, Cellular, VPN, and Offline states with a simple API and Jetpack Compose support.

connectivity(context).observe()

Features

  • WiFi and Ethernet detection
  • Cellular detection (3G / LTE / 5G)
  • VPN detection (priority over WiFi and Cellular)
  • Offline detection
  • Kotlin Flow with distinct updates
  • Jetpack Compose integration
  • One-shot snapshot via currentStatus

Installation

Maven Central publishing is planned. For now, add the library module to your project.

1. Clone the repository

git clone https://github.com/majid2851/AndroidConnect.git

Copy the connectivity folder into your Android project, or use it as a Git submodule.

2. Include the module

In settings.gradle.kts:

include(":connectivity")

If the module is in a subfolder:

include(":connectivity")
project(":connectivity").projectDir = file("path/to/connectivity")

3. Add the dependency

In app/build.gradle.kts:

dependencies {
    implementation(project(":connectivity"))
}

4. Sync Gradle

No extra permission setup is required. The library declares ACCESS_NETWORK_STATE and merges it automatically.

Quick Start

Flow API

import com.majid.connectivity.NetworkStatus
import com.majid.connectivity.connectivity
import kotlinx.coroutines.launch

val connectivity = connectivity(context)

lifecycleScope.launch {
    connectivity.observe().collect { status ->
        when (status) {
            NetworkStatus.WiFi -> { /* WiFi */ }
            NetworkStatus.Cellular -> { /* Mobile Data */ }
            NetworkStatus.Vpn -> { /* VPN */ }
            NetworkStatus.Offline -> { /* No Internet */ }
            NetworkStatus.Unknown -> { /* Initial */ }
        }
    }
}

Jetpack Compose

import androidx.compose.runtime.getValue
import com.majid.connectivity.compose.rememberConnectivityState

@Composable
fun MyScreen() {
    val status by rememberConnectivityState()

    if (!status.isConnected) {
        Text("You are offline")
    }
}

Usage

Snapshot

val status = connectivity(context).currentStatus

if (status.isConnected) {
    fetchData()
}

Compose (advanced)

import androidx.compose.runtime.getValue
import com.majid.connectivity.compose.rememberConnectivity
import androidx.lifecycle.compose.collectAsStateWithLifecycle

@Composable
fun StatusText() {
    val conn = rememberConnectivity()
    val status by conn.observe()
        .collectAsStateWithLifecycle(initialValue = conn.currentStatus)

    Text("Network: ${status.label}")
}

API

NetworkStatus

Status Description
WiFi Connected via WiFi or Ethernet
Cellular Connected via mobile data
Vpn Connected through a VPN tunnel
Offline No active network
Unknown Initial state

Helpers:

  • isConnected: Boolean
  • label: String

Connectivity

class Connectivity {
    fun observe(): Flow<NetworkStatus>
    val currentStatus: NetworkStatus
}

fun connectivity(context: Context): Connectivity

Compose helpers

@Composable
fun rememberConnectivity(context: Context = LocalContext.current): Connectivity

@Composable
fun rememberConnectivityState(context: Context = LocalContext.current): State<NetworkStatus>

Detection Priority

When multiple transports are active at the same time:

VPN > WiFi / Ethernet > Cellular > Offline

Project Structure

AndroidConnect/
|-- app/
|-- connectivity/
    |-- src/main/java/com/majid/connectivity/
        |-- Connectivity.kt
        |-- ConnectivityObserver.kt
        |-- NetworkStatus.kt
        |-- NetworkConnectivityObserver.kt
        |-- compose/ConnectivityState.kt

Demo App

Run the demo app:

./gradlew :app:installDebug

The demo includes:

  • Animated status card
  • Feature chips for WiFi, Cellular, VPN, and Offline
  • Real-time change history

Build

Linux / macOS:

./gradlew :connectivity:assembleDebug
./gradlew assembleDebug

Windows:

.\gradlew.bat assembleDebug

Requirements

Item Version
Min SDK 21
Target SDK 35
Kotlin 2.0.0
Compose BOM 2024.08.00
Gradle 8.8

Roadmap

  • Maven Central publishing
  • JitPack support
  • Callback-style API
  • Instrumented tests
  • Metered network detection

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Open a Pull Request

License

Apache License 2.0. See LICENSE.

About

Useful librray for checking internet connection of app

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages