当WIFI连接互联网如何检测? [英] How to detect when WIFI is connected to internet?

查看:357
本文介绍了当WIFI连接互联网如何检测?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个Android应用程序,我使用下面的code,以检测是否有网络连接。它运作良好,并检测移动和WIFI网络。

I am building an Android app and I use the code below to detect whether there is a network connection. It works well and detects both mobile and WIFI networks.

我的问题是如何检测一个实际的网络连接。当连接到WIFI但是WIFI不一定被连接到互联网的code以下返回true。

My problem is how to detect an actual internet connection. The code below returns true when connected to WIFI however the WIFI might not necessarily be connected to the Internet.

的code

The code

    protected boolean checkInternetConnection() {     
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);  
    // test for connection     
    if (cm.getActiveNetworkInfo() != null             
            && cm.getActiveNetworkInfo().isAvailable()             
            && cm.getActiveNetworkInfo().isConnected()) {

        return true;

        } 
    else {               
        return false;

        } 

} //end checkInterneConnection method

感谢您的时间。

梅尔

推荐答案

您应该尝试访问Internet ADRESS。因此,你应该检查InetAdress类和方法isReachable:的http://developer.android.com/reference/java/net/InetAddress.html#isReachable%28int%29

You should try to reach an internet adress. Therefor you should check the InetAdress class and the method isReachable: http://developer.android.com/reference/java/net/InetAddress.html#isReachable%28int%29

这篇关于当WIFI连接互联网如何检测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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