如何在Android中检查互联网连接?

如何在Android中检查互联网连接?,android,Android,我正在尝试正常检查互联网状态。当我启用internet并运行此应用程序时,它会显示“internet已连接”。当我停止互联网,然后再次运行此应用程序时,它将显示“未连接到互联网”。但当我在模拟器中运行这个应用程序时,它总是显示Internet已连接。请告诉我有什么问题 //check Internet connection. private boolean checkInternetConnection() { ConnectivityManager check = (Connectiv

我正在尝试正常检查互联网状态。当我启用internet并运行此应用程序时,它会显示“internet已连接”。当我停止互联网,然后再次运行此应用程序时,它将显示“未连接到互联网”。但当我在模拟器中运行这个应用程序时,它总是显示Internet已连接。请告诉我有什么问题

//check Internet connection.
private boolean checkInternetConnection() {
    ConnectivityManager check = (ConnectivityManager) this.context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (check != null) {
        NetworkInfo[] info = check.getAllNetworkInfo();
        if (info != null)  
            for (int i = 0; i <info.length; i++) 
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                    Toast.makeText(context, "Internet is connected", Toast.LENGTH_SHORT).show();
                }
             return true;
    } else {
        Toast.makeText(context, "not connected to internet", Toast.LENGTH_SHORT).show();
        return false;
    }

}
//检查Internet连接。
专用布尔值checkInternetConnection(){
ConnectivityManager check=(ConnectivityManager)this.context.getSystemService(context.CONNECTIVITY\u服务);
如果(检查!=null){
NetworkInfo[]info=check.getAllNetworkInfo();
如果(信息!=null)

对于(inti=0;i我想问题是一致的

NetworkInfo[] info = check.getAllNetworkInfo();
您将只获得活动的网络信息 你可以尝试以下我测试过的方法,效果很好:

public boolean isNetworkUp() {
    ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (activeNetwork != null && activeNetwork.isConnectedOrConnecting()) {
        if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
            return true;
        }
    }
    return false;
}

你可以试试这个方法

私有布尔值isNetConnected(){


}

请使用以下代码

/**
 * Checks if is connection.
 * 
 * @param con
 *            the con
 * @return true, if is connection
 */
public static boolean isConnection(Context con) {
    ConnectivityManager conMgr = (ConnectivityManager) con
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (conMgr.getActiveNetworkInfo() != null
            && conMgr.getActiveNetworkInfo().isAvailable()
            && conMgr.getActiveNetworkInfo().isConnected()) {
        return true;
    } else {
        return false;
    }
}

让我知道您的反馈。

您也可以使用F8设置小区网络的开/关。
检查此链接:

这将是您问题的解决方案

public boolean isNetworkAvailable() {
            ConnectivityManager connectivityManager 
                  = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
            if (activeNetworkInfo != null && activeNetworkInfo.isConnected()){ 
            return true;
            }else {
                return false;
            }
        }

然后你连接到internet这就是为什么它显示它已连接Simpley你的仿真器在你的机器上,如果你的机器有internet,那么仿真器也有internet尝试通过禁用你的机器来检查internet使用F8在仿真器中启用/禁用internet这是因为你的仿真器始终与主机PC连接,所以网络可用它没有检查实际的internet及其检查网络可用性在最近的一次编辑中,变量
checkInternetConnection
的拼写已更正。如果您将答案复制/粘贴回代码中,应确保这不会中断应用程序的其余部分。
public boolean isNetworkAvailable() {
            ConnectivityManager connectivityManager 
                  = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
            if (activeNetworkInfo != null && activeNetworkInfo.isConnected()){ 
            return true;
            }else {
                return false;
            }
        }