Skip to content Skip to sidebar Skip to footer

What Is Alternative To Connectivitymanager.type_wifi Deprecated In Android P Api 28?

ConnectivityManager.TYPE_WIFI is deprecated in Android P API 28. Also, NetworkInfo#getType and ConnectivityManager.TYPE_MOBILE's are also deprecated. So, what are the alternatives

Solution 1:

You can use below snippet to check if you have Wifi connection or Cellular:

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

Solution 2:

Use below method.. 19/06/2019

publicbooleanisconnectedToWifi(Context context) {
    ConnectivityManagerconnectivityManager= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivityManager == null) {
        returnfalse;
    }

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

Solution 3:

ConnectivityManager.TYPE_WIFI is Deprecated. You should use NetworkCapabilities.

This replaces the old ConnectivityManager.TYPE_MOBILE method of network selection. Rather than indicate a need for Wi-Fi because an application needs high bandwidth and risk obsolescence when a new, fast network appears (like LTE), the application should specify it needs high bandwidth. Similarly if an application needs an unmetered network for a bulk transfer it can specify that rather than assuming all cellular based connections are metered and all Wi-Fi based connections are not.

Applications should instead use NetworkCapabilities.hasTransport(int) or requestNetwork(NetworkRequest, NetworkCallback) to request an appropriate network. for supported transports.

You can try this way

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

Solution 4:

This is how I managed to check if WIFI is available or not without using deprecated libraries and adding support for marshamallow devices-

@ExperimentalCoroutinesApisuspendfunisWifiAvailable(): Boolean {
        val connectivityManager =
            ContextCompat.getSystemService(context, ConnectivityManager::class.java) ?: returnfalseif (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val networkCapabilities = connectivityManager.activeNetwork ?: returnfalseval activeNetwork =
                connectivityManager.getNetworkCapabilities(networkCapabilities) ?: returnfalsereturn activeNetwork.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)
        } else {
            return connectivityManager.isNetworkAvailable(NetworkCapabilities.TRANSPORT_WIFI)
//            val activeNetwork = connectivityManager.`activeNetworkInfo`//            return activeNetwork?.type == ConnectivityManager.TYPE_WIFI
        }
    }

    @ExperimentalCoroutinesApiprivatesuspendfun 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() {
                overridefunonAvailable(network: Network) {
                    super.onAvailable(network)
                    continuation.resume(true, null)
                    unregisterNetworkCallback(this)
                }

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

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

Post a Comment for "What Is Alternative To Connectivitymanager.type_wifi Deprecated In Android P Api 28?"