1

更新:基本上我需要确认我的网络是私有网络还是与外界连接,即连接到互联网。例如,我在 WiFi 网络上,但未连接路由器互联网线路。因此,虽然网络已连接,但互联网不可用。

需要在我的 Android 应用程序中显示网络详细信息,

  • (a) n/w 是否可用
  • (b) 互联网设施是否可用

目前,我认为 www.google.com 将始终处于上升和低于代码部分的状态。但是这个解决方案是缓慢的,并且基于 www.google.com 将始终处于运行状态的假设。

有没有更好的选择来确认网络上是否有互联网设施?

public static boolean isConnected(Context context){
        NetworkInfo info = Connectivity.getNetworkInfo(context);
        return (info != null && info.isConnected());
    }

public boolean checkInternetConnectivity() {

        try {
            HttpURLConnection urlc = (HttpURLConnection) (new URL(
                    "http://www.google.com").openConnection());
            urlc.setRequestProperty("User-Agent", "Test");
            urlc.setRequestProperty("Connection", "close");
            urlc.setConnectTimeout(3000);
            urlc.setReadTimeout(4000);
            urlc.connect();
            isInterNetAvailable = true;
            return (urlc.getResponseCode() == 200);
        } catch (IOException e) {
            isInterNetAvailable = false;
            return (false);
        }
    }
public String getNWConnectivityText() {
    if (isConnected(getSherlockActivity()) == true) {

        if (true == checkInternetConnectivity()) {
            return ""N/W is connected and Internet is working!";
        } else {
            return "N/W is connected but Internet is NOT working!";
        }

    } else {
        return "N/W is not connected!";
    }
}
4

4 回答 4

1
public static boolean isConnected(Context context) {
    final ConnectivityManager conMgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
    if (activeNetwork != null && activeNetwork.getState() == NetworkInfo.State.CONNECTED) {
        return true;
    } else {
        return false;
    }
}

添加到清单文件:

<uses-permission android:name="android.permission.INTERNET" />
于 2013-11-02T10:15:06.007 回答
0

我实际上总是使用这个 ConnectionController 类。它检查WiFi移动连接

public class ConnectionController {

    /** NEEDS PERMISSIONS NETWORK AND WIFI STATE */

    /**
     * checks if a mobile or wifi connection is available or about to be available
     * sends a proper response for user information
     * @param context
     * @return
     */
    public static boolean isConnected(Context context) {

        if(context != null) {

            ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo netinfo = cm.getActiveNetworkInfo();

            if (netinfo != null && netinfo.isConnectedOrConnecting()) {
                android.net.NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                android.net.NetworkInfo mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

                if((mobile != null && mobile.isConnectedOrConnecting()) || (wifi != null && wifi.isConnectedOrConnecting())) return true;
                else return false;
            } else
                return false;
        } else return false;
    }
}

在代码中,您可以像这样检查您的连接:

if(ConnectionController.isConnected(Context)) {
    // do something if connection available
}
于 2013-11-02T09:32:53.487 回答
0

您可以使用此方法检查互联网连接

public boolean isConnectingToInternet(){
        ConnectivityManager connectivity = (ConnectivityManager) 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;
    }

在清单文件中还包括以下权限:

<uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

我希望这可以帮助你。

于 2013-11-02T09:38:57.503 回答
0

使用此方法从您的活动中检查互联网连接

public boolean isInternetConnected() {
        ConnectivityManager cManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cManager.getActiveNetworkInfo();

        if(networkInfo == null)
            return false;
        else if(!networkInfo.isConnected())
            return false;
        else if(!networkInfo.isAvailable())
            return false;
        else
            return true;
    }

另外,添加这些权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
于 2013-11-02T09:23:55.140 回答