互联网连接检查在android系统发生故障 [英] Internet connectivity check fails in android

查看:153
本文介绍了互联网连接检查在android系统发生故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作,需要与服务器进行通信的应用程序。因此,对于互联网连接的检查落实。

该检查工作完全正常,当我移动数据和WiFi的工作。

问题是我的设备与热点连接。我点击登录按钮并获得指出连接到服务器进度条。现在,我关掉热点。但WiFi是在我的设备启用。

(它只是没有得到任何回应code)。

我的应用程序进入无限的状态。

如何解决这个问题的任何建议。


解决方案

 公共类checkconnection {    公共静态布尔checkInternetConnection(上下文的背景下){
        布尔haveConnectedWifi = FALSE;
        布尔haveConnectedMobile = FALSE;        ConnectivityManager厘米=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        尝试{
            的NetworkInfo [] = NetInfo的cm.getAllNetworkInfo();
            对于(NI的NetworkInfo:NetInfo的){
                如果(ni.getTypeName()。equalsIgnoreCase(WIFI))
                    如果(ni.isConnected())
                        haveConnectedWifi = TRUE;
                如果(ni.getTypeName()。equalsIgnoreCase(手机))
                    如果(ni.isConnected())
                        haveConnectedMobile = TRUE;
            }
        }赶上(例外五){
            e.getStackTrace();
        }
        返回haveConnectedWifi || haveConnectedMobile;
    }
}

您拨打登录之前,请使用以下 checkInternetConnection()方法在你的类。 真正确保您已连接。

I am working on an application that needs to communicate with the server. Hence, the check for the internet connectivity is implemented.

The check works perfectly fine when I am working with mobile data and wifi.

The issue is my device is connected with hotspot. I click on the login button and get the progress bar that states "Connecting to the server". Now, I turn off the hotspot. But the wifi is enabled in my device.

(It simply fails to get any responseCode).

My application gets into the infinite state.

Any suggestions of how to overcome this problem.

解决方案

public class checkconnection {

    public static boolean checkInternetConnection(Context context) {
        boolean haveConnectedWifi = false;
        boolean haveConnectedMobile = false;

        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        try {
            NetworkInfo[] netInfo = cm.getAllNetworkInfo();
            for (NetworkInfo ni : netInfo) {
                if (ni.getTypeName().equalsIgnoreCase("WIFI"))
                    if (ni.isConnected())
                        haveConnectedWifi = true;
                if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
                    if (ni.isConnected())
                        haveConnectedMobile = true;
            }
        } catch (Exception e) {
            e.getStackTrace();
        }
        return haveConnectedWifi || haveConnectedMobile;
    }


}

Use the following checkInternetConnection() method in your class before you call the login. true ensures that you have connectivity.

这篇关于互联网连接检查在android系统发生故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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