且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

getAllNetworkInfo()是pcated德$ P $。如何使用getAllNetworks()来检查网络连接的android?

更新时间:2023-11-08 19:02:28

在更新我去precated code和还是要支持向后API。我使用这样的:

 如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.WANTED API版本){
// code
}其他{
// code
}
 

在这种方式,每个设备使用适当的code他。 例如:

 公共类ConnectionDetector {

    私人语境mContext;

    公共ConnectionDetector(上下文的背景下){
        this.mContext =背景;
    }
    / **
     *检查所有可能的互联网服务供应商
     * ** /
    公共布尔isConnectingToInternet(){
        ConnectivityManager connectivityManager =(ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.LOLLIPOP){
            网络[]网络= connectivityManager.getAllNetworks();
            的NetworkInfo的NetworkInfo;
            对于(网络mNetwork:网络){
                的NetworkInfo = connectivityManager.getNetworkInfo(mNetwork);
                如果(networkInfo.getState()。等于(NetworkInfo.State.CONNECTED)){
                    返回true;
                }
            }
        }其他 {
            如果(connectivityManager!= NULL){
                // noinspection德precation
                的NetworkInfo []信息= connectivityManager.getAllNetworkInfo();
                如果(资讯!= NULL){
                    对于(的NetworkInfo anInfo:信息){
                        如果(anInfo.getState()== NetworkInfo.State.CONNECTED){
                            LogUtils.d(网络,
                                    NETWORKNAME:+ anInfo.getTypeName());
                            返回true;
                        }
                    }
                }
            }
        }
        Toast.makeText(mContext,mContext.getString(R.string.please_connect_to_internet),Toast.LENGTH_SHORT).show();
        返回false;
    }
}
 

I want to use the Connectivity manager which provide the method getAllNetworkInfo() for checking the availability of network in Android. This method was deprecated in API level 23. And Developer doc is suggesting to use getAllNetworks() instead. I tried but counldn't get the exact functionalities I was getting out of my old code. Please someone could guide me how to use getAllNetworks() method. Below is the code which I'm using:

 public boolean isConnectingToInternet(){
    ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
      if (connectivity != null) 
      {
          @SuppressWarnings("deprecation")
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
          //use getAllNetworks() instead
          if (info != null) 
              for (int i = 0; i < info.length; i++) 
                  if (info[i].getState() == NetworkInfo.State.CONNECTED)
                  {
                      return true;
                  }
      }
      return false;
}

When i update my deprecated code and still want to support backward Api. i use this :

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.WANTED API VERSION){
//code
}else{
//code
}

In this way each device use the appropriate code for him. Example:

public class ConnectionDetector {

    private Context mContext;

    public ConnectionDetector(Context context) {
        this.mContext = context;
    }
    /**
     * Checking for all possible internet providers
     * **/
    public boolean isConnectingToInternet() {
        ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Network[] networks = connectivityManager.getAllNetworks();
            NetworkInfo networkInfo;
            for (Network mNetwork : networks) {
                networkInfo = connectivityManager.getNetworkInfo(mNetwork);
                if (networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) {
                    return true;
                }
            }
        }else {
            if (connectivityManager != null) {
                //noinspection deprecation
                NetworkInfo[] info = connectivityManager.getAllNetworkInfo();
                if (info != null) {
                    for (NetworkInfo anInfo : info) {
                        if (anInfo.getState() == NetworkInfo.State.CONNECTED) {
                            LogUtils.d("Network",
                                    "NETWORKNAME: " + anInfo.getTypeName());
                            return true;
                        }
                    }
                }
            }
        }
        Toast.makeText(mContext,mContext.getString(R.string.please_connect_to_internet),Toast.LENGTH_SHORT).show();
        return false;
    }
}