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

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

    Try this one .It is the simplest way.

    public static boolean isNetworkAvailable(Activity activity) {  
            ConnectivityManager connectivity = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);  
            if (connectivity == null) {  
                return false;  
            } else {  
                NetworkInfo[] info = connectivity.getAllNetworkInfo();  
                if (info != null) {  
                    for (int i = 0; i < info.length; i++) {  
                        if (info[i].getState() == NetworkInfo.State.CONNECTED) {  
                            return true;  
                        }  
                    }  
                }  
            }  
            return false;  
        }  
    }  
    

提交回复
热议问题