gpt4 book ai didi

android - 查询 ConnectivityManager getActiveNetwork

转载 作者:太空狗 更新时间:2023-10-29 13:49:39 26 4
gpt4 key购买 nike

需要知道getActiveNetwork返回什么样的值。我使用的代码是:

    ConnectivityManager connectivityManager =  (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);    
Network network = connectivityManager.getActiveNetwork();
Log.e(TAG,"Network : " + network.toString());

输出返回数字,这个数字到底意味着什么?

最佳答案

很好,许多人提到在 6.0 SDK 版本之前不推荐使用 connectivityManager.activeNetworkInfo.typeconnectivityManager.activeNetworkInfo.typeName

我遇到过类似的情况,必须同时支持最低 5.0 和 >= 6.0 的设备。这对我有用。

if (SDK_INT >= M) {
connectivityManager.activeNetwork
?.let { connectivityManager.getNetworkCapabilities(it) }
?.takeIf { it.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) }
?.takeIf { connectivityManager.activeNetworkInfo.isConnected }
?.let { return "Wifi is Connected"
//Just to make a point. Better be a sealed class.
}
} else {
connectivityManager.activeNetworkInfo
?.takeIf { it.isConnected && it.type == ConnectivityManager.TYPE_WIFI }
?.let { return "Wifi is Connected" }
}

return "Wifi is Disconnected"

关于android - 查询 ConnectivityManager getActiveNetwork,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49812761/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com