Android获取网络类型

 val connectivityManager: ConnectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            val network = connectivityManager.activeNetwork
            val networkCapabilities  = connectivityManager.getNetworkCapabilities(network)
            if (networkCapabilities == null) {
                Toast.makeText(context,"无网络链接",Toast.LENGTH_LONG).show()
            } else if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
                Toast.makeText(context,"流量",Toast.LENGTH_LONG).show()
            } else if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
                Toast.makeText(context, "WIFI", Toast.LENGTH_LONG).show()
            }

你可能感兴趣的:(Android工具类,android)