小编Ati*_*man的帖子

Kotlin 静态扩展函数 Android

我想为 Android 内置ConnectivityManager类创建一个扩展函数,以便我的项目中的任何类都可以静态使用它。我只使用 Kotlin。

fun ConnectivityManager.checkInternet(context: Context): Boolean {
        val manager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val connection = manager.activeNetworkInfo
        if (connection != null && connection.isConnectedOrConnecting) {
            return true
        }
        return false
    }
Run Code Online (Sandbox Code Playgroud)

我可以通过创建 的对象来访问此函数,ConnectivityManager但我想做的是静态使用它。有什么可能吗?

android kotlin

1
推荐指数
2
解决办法
2454
查看次数

标签 统计

android ×1

kotlin ×1