测试网络

//测试网络
fun initNetStatus(){
    homeNetStatus.setOnClickListener {
        //获取连接管理器对象
        val connectivityManager: ConnectivityManager = context!!.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        //获取网络信息
        val activeNetwork = connectivityManager.activeNetworkInfo
        //判断网络处于连接状态
        if (activeNetwork !=null && activeNetwork.isConnected){
            Log.i("TAG","有连接")
            if (activeNetwork.type == ConnectivityManager.TYPE_WIFI){
                Log.i("TAG","wifi连接")
            }else if (activeNetwork.type == ConnectivityManager.TYPE_MOBILE){
                Log.i("TAG","方法连接")
            }
        }else{
            Log.i("TAG","无连接")
        }
    }
}

你可能感兴趣的:(Studio方法)