Kotlin 实现 Android 网络状态检测工具类

以下是一个完整的 Android Kotlin 网络连接检测工具类:

  1. 添加权限

首先在 AndroidManifest.xml 中添加网络权限:

xml 复制代码
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  1. 网络连接工具类
kotlin 复制代码
import android.content.Context
import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkCapabilities
import android.net.NetworkRequest
import android.os.Build
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow

/**
 * 网络连接状态工具类
 * 支持实时监听网络状态变化
 */
object NetworkUtils {

    private var connectivityManager: ConnectivityManager? = null
    
    // 网络状态流,可以观察网络状态变化
    private val _networkState = MutableStateFlow(false)
    val networkState: StateFlow<Boolean> = _networkState

    /**
     * 初始化工具类
     */
    fun initialize(context: Context) {
        connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as? ConnectivityManager
        updateNetworkState(context)
        registerNetworkCallback(context)
    }

    /**
     * 检查是否连接网络
     */
    fun isNetworkConnected(context: Context): Boolean {
        return try {
            val cm = connectivityManager ?: context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                val network = cm.activeNetwork
                val capabilities = cm.getNetworkCapabilities(network)
                capabilities != null && (
                    capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) ||
                    capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) ||
                    capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)
                )
            } else {
                @Suppress("DEPRECATION")
                val networkInfo = cm.activeNetworkInfo
                networkInfo != null && networkInfo.isConnected
            }
        } catch (e: Exception) {
            e.printStackTrace()
            false
        }
    }

    /**
     * 检查是否是 WiFi 连接
     */
    fun isWifiConnected(context: Context): Boolean {
        return try {
            val cm = connectivityManager ?: context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                val network = cm.activeNetwork
                val capabilities = cm.getNetworkCapabilities(network)
                capabilities?.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) == true
            } else {
                @Suppress("DEPRECATION")
                val networkInfo = cm.activeNetworkInfo
                networkInfo != null && networkInfo.isConnected && networkInfo.type == ConnectivityManager.TYPE_WIFI
            }
        } catch (e: Exception) {
            e.printStackTrace()
            false
        }
    }

    /**
     * 检查是否是移动数据连接
     */
    fun isMobileDataConnected(context: Context): Boolean {
        return try {
            val cm = connectivityManager ?: context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                val network = cm.activeNetwork
                val capabilities = cm.getNetworkCapabilities(network)
                capabilities?.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) == true
            } else {
                @Suppress("DEPRECATION")
                val networkInfo = cm.activeNetworkInfo
                networkInfo != null && networkInfo.isConnected && networkInfo.type == ConnectivityManager.TYPE_MOBILE
            }
        } catch (e: Exception) {
            e.printStackTrace()
            false
        }
    }

    /**
     * 获取网络类型
     */
    fun getNetworkType(context: Context): String {
        return when {
            isWifiConnected(context) -> "WiFi"
            isMobileDataConnected(context) -> "Mobile"
            else -> "No Network"
        }
    }

    /**
     * 注册网络状态监听
     */
    private fun registerNetworkCallback(context: Context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            val cm = connectivityManager ?: return
            
            val networkRequest = NetworkRequest.Builder()
                .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
                .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
                .addTransportType(NetworkCapabilities.TRANSPORT_ETHERNET)
                .build()

            cm.registerNetworkCallback(networkRequest, object : ConnectivityManager.NetworkCallback() {
                override fun onAvailable(network: Network) {
                    _networkState.value = true
                }

                override fun onLost(network: Network) {
                    _networkState.value = false
                }

                override fun onUnavailable() {
                    _networkState.value = false
                }
            })
        }
    }

    /**
     * 更新网络状态
     */
    private fun updateNetworkState(context: Context) {
        _networkState.value = isNetworkConnected(context)
    }
}
  1. 扩展函数
kotlin 复制代码
/**
 * Context 的扩展函数,简化调用
 */
fun Context.isNetworkConnected(): Boolean {
    return NetworkUtils.isNetworkConnected(this)
}

fun Context.isWifiConnected(): Boolean {
    return NetworkUtils.isWifiConnected(this)
}

fun Context.isMobileDataConnected(): Boolean {
    return NetworkUtils.isMobileDataConnected(this)
}

fun Context.getNetworkType(): String {
    return NetworkUtils.getNetworkType(this)
}
  1. 使用示例

在 Application 中初始化:

kotlin 复制代码
class MyApplication : Application() {
    override fun onCreate() {
        super.onCreate()
        NetworkUtils.initialize(this)
    }
}

在 Activity/Fragment 中使用:

kotlin 复制代码
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // 基本使用
        checkNetworkStatus()
        
        // 观察网络状态变化
        observeNetworkState()
    }

    /**
     * 检查当前网络状态
     */
    private fun checkNetworkStatus() {
        if (isNetworkConnected()) {
            val networkType = getNetworkType()
            Toast.makeText(this, "网络已连接 - $networkType", Toast.LENGTH_SHORT).show()
        } else {
            Toast.makeText(this, "网络未连接", Toast.LENGTH_SHORT).show()
        }

        // 或者使用工具类直接调用
        if (NetworkUtils.isNetworkConnected(this)) {
            // 执行网络请求
        }
    }

    /**
     * 观察网络状态变化
     */
    private fun observeNetworkState() {
        lifecycleScope.launch {
            NetworkUtils.networkState.collect { isConnected ->
                if (isConnected) {
                    // 网络恢复
                    showNetworkRestored()
                } else {
                    // 网络断开
                    showNetworkLost()
                }
            }
        }
    }

    private fun showNetworkRestored() {
        // 显示网络恢复提示
        Snackbar.make(findViewById(android.R.id.content), 
            "网络已恢复", Snackbar.LENGTH_SHORT).show()
    }

    private fun showNetworkLost() {
        // 显示网络断开提示
        Snackbar.make(findViewById(android.R.id.content), 
            "网络连接已断开", Snackbar.LENGTH_LONG).show()
    }
}

在网络请求前检查:

kotlin 复制代码
class UserRepository(private val context: Context) {
    
    suspend fun fetchUserData(): Result<User> {
        // 在发起网络请求前检查网络
        if (!context.isNetworkConnected()) {
            return Result.failure(IOException("网络未连接"))
        }
        
        return try {
            // 执行网络请求
            val response = apiService.getUser()
            Result.success(response)
        } catch (e: Exception) {
            Result.failure(e)
        }
    }
}
  1. 高级功能 - 网络状态监听器

如果需要更详细的回调,可以添加监听器:

kotlin 复制代码
interface NetworkStateListener {
    fun onNetworkAvailable(networkType: String)
    fun onNetworkLost()
    fun onNetworkTypeChanged(networkType: String)
}

class AdvancedNetworkMonitor(private val context: Context) {
    private val listeners = mutableSetOf<NetworkStateListener>()
    private var lastNetworkType = ""
    
    init {
        observeNetworkState()
    }
    
    private fun observeNetworkState() {
        lifecycleScope.launch {
            NetworkUtils.networkState.collect { isConnected ->
                val currentType = context.getNetworkType()
                
                if (isConnected) {
                    listeners.forEach { it.onNetworkAvailable(currentType) }
                    if (lastNetworkType != currentType) {
                        listeners.forEach { it.onNetworkTypeChanged(currentType) }
                    }
                } else {
                    listeners.forEach { it.onNetworkLost() }
                }
                
                lastNetworkType = currentType
            }
        }
    }
    
    fun addListener(listener: NetworkStateListener) {
        listeners.add(listener)
    }
    
    fun removeListener(listener: NetworkStateListener) {
        listeners.remove(listener)
    }
}

这个工具类提供了:

· ✅ 检测网络连接状态

· ✅ 区分 WiFi 和移动数据

· ✅ 实时网络状态监听

· ✅ 简洁的 API 调用

· ✅ 支持协程 StateFlow

· ✅ 兼容不同 Android 版本

可以直接在项目中使用,确保在网络请求前进行连接检查!

相关推荐
河北瑾航科技11 分钟前
广西水资源遥测终端 广西水利遥测终端 广西用水监测遥测终端 河北瑾航科技遥测终端机HBJH-B01说明书
网络·科技·水文遥测终端机·遥测终端机·广西水资源遥测终端机·广西水利遥测终端·广西用水终端
fouryears_2341736 分钟前
现代 Android 后台应用读取剪贴板最佳实践
android·前端·flutter·dart
羑悻的小杀马特1 小时前
轻量跨云·掌控无界:Portainer CE + cpolar 让远程容器运维像点外卖一样简单——免复杂配置,安全直达对应集群
运维·网络·安全·docker·cpolar
愚戏师2 小时前
Python3 Socket 网络编程复习笔记
网络·笔记
YF02112 小时前
Frida for MacBook/Android 安装配置
android·前端
雨白2 小时前
Android实战:构建高可维护的日志系统
android
降临-max2 小时前
JavaSE---网络编程
java·开发语言·网络·笔记·学习
赖small强3 小时前
【Linux 网络基础】libwebsockets HTTPS 服务端实现机制详解
linux·网络·https·tls·libwebsockets
大白的编程日记.3 小时前
【计算网络学习笔记】MySql的多版本控制MVCC和Read View
网络·笔记·学习·mysql
茄子凉心3 小时前
android 开机启动App
android·java·开发语言