I have little issue with detecting internet connection while using static IP on device. While I'm connecting without setting any static ip my function for detecting available connection is working properly. But when I set static IP, the function return true, because I'm connected to Wifi or 3G, but I don't have any internet connection and so my app crashes in that situation. Any ideas how to fix that problem while using static ip? Here is what I'm using :

public boolean chkNetworkStatus(Context context) {
     ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
     if (connectivity == null) {
         Toast.makeText(context, "No available connection!", Toast.LENGTH_LONG);
     } 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; 
}
有帮助吗?

解决方案 2

Here is what you can use in your situation. This function will return true if you are connected to wifi/3g and you can load any web page.

    public static boolean isOnline(Context context) {
         ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
         NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
            return true;
        }
        return false;
    }

    public static boolean chkNetworkStatus(Context context) {
    boolean result = false;
    new Thread() {
        @Override
        public void run() {


           for(int i=0;i<3;i++){
           HttpGet requestForTest = new HttpGet("http://m.google.com");
           try {
                  new DefaultHttpClient().execute(requestForTest);
                  responded = true;
                } catch (Exception e) {
                    responded = false;
                }
           }
        }
    }.start();
    boolean isOnline = isOnline(context);
    if(responded && isOnline){
        result = true;
    } else {
        result = false;
    }

    Log.e("","responded : "+responded);
    return result;
}

其他提示

Use this method.... I hope it will helpfull for you.

public boolean checkNetworkConnection(Context context) {

    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();

    if (activeNetwork != null) {
        return activeNetwork.isConnectedOrConnecting();
    }

    NetworkInfo wifiNetwork = cm
            .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifiNetwork != null) {
        return wifiNetwork.isConnectedOrConnecting();
    }

    NetworkInfo mobileNetwork = cm
            .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (mobileNetwork != null) {
        return mobileNetwork.isConnectedOrConnecting();
    }

    NetworkInfo otherNetwork = cm.getActiveNetworkInfo();
    if (otherNetwork != null) {
        return otherNetwork.isConnectedOrConnecting();
    }
    return false;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top