Android 在使用静态 IP 时检查网络连接

发布于 2024-12-25 10:09:57 字数 840 浏览 2 评论 0原文

在设备上使用静态 IP 时,我在检测互联网连接方面没有什么问题。当我在没有设置任何静态 IP 的情况下进行连接时,我用于检测可用连接的功能正常工作。但是当我设置静态 IP 时,该函数返回 true,因为我已连接到 Wifi 或 3G,但我没有任何互联网连接,因此我的应用程序在这种情况下崩溃了。有什么想法如何在使用静态 IP 时解决该问题吗? 这是我正在使用的:

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; 
}

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; 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半窗疏影 2025-01-01 10:09:57

使用这个方法......我希望它对你有帮助。

公共布尔checkNetworkConnection(上下文上下文){

    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;
}

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;
}
情绪 2025-01-01 10:09:57

以下是您可以根据自己的情况使用的内容。如果您连接到 wifi/3g 并且可以加载任何网页,此函数将返回 true

    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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文