Android getAllNetworkInfo() is Deprecated. What is the alternative?

后端 未结 6 1588
生来不讨喜
生来不讨喜 2021-01-31 09:33

I want to use the ConnectivityManager which provides the getAllNetworkInfo() method for checking the availability of network in Android. This method wa

6条回答
  •  你的背包
    2021-01-31 09:36

    Try following code:

    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);    
        Network[] networks = connectivityManager.getAllNetworks();
        NetworkInfo networkInfo;
        Network network;
            for (int i = 0; i < networks.length; i++){               
                network = networks[i];
                networkInfo = connectivityManager.getNetworkInfo(network);
                if ((networkInfo.getType() ==     ConnectivityManager.TYPE_WIFI) && (networkInfo.getState().equals(NetworkInfo.State.CONNECTED))) {
                   ConnectivityManager.setProcessDefaultNetwork(network);
                    break;
                }
            }
    

提交回复
热议问题