kyleED kyleED - 1 month ago 19
Android Question

ConnectivityManager getNetworkInfo(int) deprecated

Using compileSdkVersion 23, however trying to support as far back as 9.

getNetworkInfo(int) was deprecated in 23. The suggestion was to use getAllNetworks and getNetworkInfo(Network) instead. However both of these require minimum of API 21.

Is there a class that we can use in the support package that can assist with this?

I know that a solution was proposed before, however the challenge of my minimum api requirements of 9 poses a problem.

Answer

You can use:

getActiveNetworkInfo();

        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        if (activeNetwork != null) { // connected to the internet
            if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
                // connected to wifi
                Toast.makeText(context, activeNetwork.getTypeName(), Toast.LENGTH_SHORT).show();
            } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
                // connected to the mobile provider's data plan
                Toast.makeText(context, activeNetwork.getTypeName(), Toast.LENGTH_SHORT).show();
            }
        } else {
            // not connected to the internet
        }
Comments