什么是Android P API 28中不推荐使用的ConnectivityManager.TYPE_WIFI的替代方法?

时间:2018-10-15 12:08:53

标签: android android-sdk-manager android-api-levels

ConnectivityManager.TYPE_WIFI在Android P API 28中已弃用。此外,NetworkInfo#getTypeConnectivityManager.TYPE_MOBILE也已弃用。

那么,有哪些替代方案呢?我了解我们必须使用 NetworkCapabilities 类中的方法。但是,我无法将所有内容合并到一个地方,例如如何在NetworkCapabilities类中进行getType()以及如何在其上添加WIFI和蜂窝数据检查?

请协助。

4 个答案:

答案 0 :(得分:4)

您可以使用以下代码段检查您是否具有Wifi连接或蜂窝网络:

ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
    Network network = connectivityManager.getActiveNetwork();
    NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network);
    return capabilities != null && (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR));
}

答案 1 :(得分:2)

ConnectivityManager.TYPE_WIFI 已弃用 。您应该使用NetworkCapabilities

这取代了旧的 ConnectivityManager.TYPE_MOBILE 网络选择方法。该应用程序应指定需要高带宽,而不是因为应用程序需要高带宽并在出现新的快速网络(如LTE)时有过时的风险而表明需要Wi-Fi,而是应指定它需要高带宽。同样,如果应用程序需要不限流量的网络进行批量传输,则可以指定,而不是假设对所有基于蜂窝的连接都进行了计费,而对所有基于Wi-Fi的连接都没有进行计费。

  

应用程序应改用NetworkCapabilities.hasTransport(int)   或requestNetwork(NetworkRequest, NetworkCallback)要求   适当的网络。支持的运输。

您可以尝试这种方式

 NetworkAgentInfo networkAgent;
        int type = ConnectivityManager.TYPE_NONE;
        if (networkAgent.networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
             type = ConnectivityManager.TYPE_MOBILE;
        } else if (networkAgent.networkCapabilities.hasTransport(
                NetworkCapabilities.TRANSPORT_WIFI)) {
                type = ConnectivityManager.TYPE_WIFI;
        }

答案 2 :(得分:2)

使用以下方法。.19/06/2019

public boolean isconnectedToWifi(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivityManager == null) {
        return false;
    }

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
        Network network = connectivityManager.getActiveNetwork();
        NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network);
        if (capabilities == null) {
            return false;
        }
        return capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI);
    } else {
        NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (networkInfo == null) {
            return false;
        }
        return networkInfo.isConnected();
    }
}

答案 3 :(得分:0)

这就是我在不使用弃用的库和添加对 marshamallow 设备的支持的情况下设法检查 WIFI 是否可用的方法-

@ExperimentalCoroutinesApi
    suspend fun isWifiAvailable(): Boolean {
        val connectivityManager =
            ContextCompat.getSystemService(context, ConnectivityManager::class.java) ?: return false
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val networkCapabilities = connectivityManager.activeNetwork ?: return false
            val activeNetwork =
                connectivityManager.getNetworkCapabilities(networkCapabilities) ?: return false
            return activeNetwork.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)
        } else {
            return connectivityManager.isNetworkAvailable(NetworkCapabilities.TRANSPORT_WIFI)
//            val activeNetwork = connectivityManager.`activeNetworkInfo`
//            return activeNetwork?.type == ConnectivityManager.TYPE_WIFI
        }
    }

    @ExperimentalCoroutinesApi
    private suspend fun ConnectivityManager.isNetworkAvailable(vararg transportType: Int): Boolean {
        return suspendCancellableCoroutine { continuation ->
            val builder = NetworkRequest.Builder()
            transportType.forEach {
                builder.addCapability(it)
            }
            val networkRequest = builder.build()
            val networkCallback = object : ConnectivityManager.NetworkCallback() {
                override fun onAvailable(network: Network) {
                    super.onAvailable(network)
                    continuation.resume(true, null)
                    unregisterNetworkCallback(this)
                }

                override fun onLost(network: Network) {
                    super.onLost(network)
                    continuation.resume(false, null)
                    unregisterNetworkCallback(this)
                }

                override fun onUnavailable() {
                    super.onUnavailable()
                    continuation.resume(false, null)
                    unregisterNetworkCallback(this)
                }
            }
            registerNetworkCallback(networkRequest, networkCallback)
        }
    }