getAllNetworkInfo()是pcated德$ P $。如何使用getAllNetworks()来检查网络连接的android? [英] getAllNetworkInfo() is Deprecated. How to use getAllNetworks() to check network connection android?

查看:642
本文介绍了getAllNetworkInfo()是pcated德$ P $。如何使用getAllNetworks()来检查网络连接的android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要使用的连接管理器,它提供的方法getAllNetworkInfo()用于检查网络在Android上的可用性。这种方法是pcated在API层面23日$ P $和开发者文档中建议使用getAllNetworks()来代替。我试过,但counldn't得到确切的功能我得到了我的老code。请有人能指导我如何使用getAllNetworks()方法。下面是我使用的code:

 公共布尔isConnectingToInternet(){
    ConnectivityManager连接=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
      如果(连接!= NULL)
      {
          @燮pressWarnings(德precation)
        的NetworkInfo []信息= connectivity.getAllNetworkInfo();
          //使用getAllNetworks(),而不是
          如果(资讯!= NULL)
              的for(int i = 0; I< info.length;我++)
                  如果(资讯[I] .getState()== NetworkInfo.State.CONNECTED)
                  {
                      返回true;
                  }
      }
      返回false;
}
 

解决方案

在更新我去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;
    }
}

这篇关于getAllNetworkInfo()是pcated德$ P $。如何使用getAllNetworks()来检查网络连接的android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆