需要知道 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/

10-12 01:51