使用getActiveNetworkInfo检查android中的互联网连接

Eda*_*izi 0 android internet-connection

如果您想在android中检查互联网连接,则有很多方法可以做到这一点,例如:

   public boolean isConnectingToInternet(){
    ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
      if (connectivity != null) 
      {
          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;
    }
Run Code Online (Sandbox Code Playgroud)

getAllNetworkInfo(); 在api级别23中已弃用,因此我需要一种解决方案来检查api级别23和以前的互联网连接,我发现了另一种方法

 private boolean isConnectedToInternet()
{
    ConnectivityManager cm = (ConnectivityManager) SplashActivity.this.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        if (activeNetwork != null) { // connected to the internet
            if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
                // connected to wifi
                Toast.makeText(SplashActivity.this, activeNetwork.getTypeName(), Toast.LENGTH_SHORT).show();
                return true;
            } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
                // connected to the mobile provider's data plan
                Toast.makeText(SplashActivity.this, activeNetwork.getTypeName(), Toast.LENGTH_SHORT).show();
                return true;
            }
        } 
    return false;
}
Run Code Online (Sandbox Code Playgroud)

我的问题是关于getActiveNetworkInfo()和isConnected()谷歌为此方法提供的文档在这里

返回有关当前活动的默认数据网络的详细信息。连接后,此网络是传出连接的默认路由。在启动网络流量之前,应始终检查isConnected()。如果没有默认网络,则可能返回null。

为什么我们需要在getActiveNetworkInfo ()上调用isConnected()来检查互联网连接,而我们可以使用null检查来自getActiveNetworkInfo()的返回值(如果不为null ),那么我们有互联网连接,否则,如果返回null则在getActiveNetworkInfo()上调用isConnected()会抛出空指针运行时异常

Ozg*_*gur 6

使用我的util类:

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;

/**
 * Created by ozgur on 16.04.2016.
 */
public class Utils {

    public static boolean isConnected(Context context){
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {

            Network[] activeNetworks = cm.getAllNetworks();
            for (Network n: activeNetworks) {
                NetworkInfo nInfo = cm.getNetworkInfo(n);
                if(nInfo.isConnected())
                    return true;
            }

        } else {
            NetworkInfo[] info = cm.getAllNetworkInfo();
            if (info != null)
                for (NetworkInfo anInfo : info)
                    if (anInfo.getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
        }

        return false;

    }
}
Run Code Online (Sandbox Code Playgroud)