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

后端 未结 6 1510
生来不讨喜
生来不讨喜 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:35

    Try this

    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    Network[] networks = cm.getAllNetworks();
    if (cm != null) {
        for (Network netinfo : networks) {
            NetworkInfo ni = cm.getNetworkInfo(netinfo);
            if (ni.isConnected() && ni.isAvailable()) {
                    connected = true;
                }
            }
        }
    

提交回复
热议问题