Android 判断是否连接网络

连接网络首先必须的就是设置权限,在配置清单文件中添加网络请求权限


 fun isNetworkConnected() : Boolean{
        val mConnectManager : ConnectivityManager = this.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val network = mConnectManager.getNetworkCapabilities(mConnectManager.activeNetwork)
        return if (network != null){
            true
        }else{
            false
        }
    }

你可能感兴趣的:(Android 判断是否连接网络)