如何知道设备是否有数据连接?

时间:2012-12-01 18:13:21

标签: android connectivity

我正在尝试检测设备是否有wifi或3g连接。如果它有一个连接,它将显示MapView,但是当两个连接都没有连接时,我的条件语句出现问题。

以下是我检查网络连接的方法:

private boolean haveNetworkConnection() {
    boolean haveConnectedWifi = false;
    boolean haveConnectedMobile = false;

    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    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;
    }
    return haveConnectedWifi || haveConnectedMobile;
}

这是我调用该方法的代码。它在有互联网访问权限时显示MapView,但在没有互联网访问权限时显示吐司。

if (haveNetworkConnection()) {
    if (loc != null && !loc.equals("")) {
        new GeocoderTask().execute(loc);
    } else {
        Toast.makeText(this, "Internet Connection is Required to view this map", Toast.LENGTH_LONG).show();
    }
}

4 个答案:

答案 0 :(得分:0)

尝试此操作以检查互联网连接是否可用:

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager 
        = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();

    return activeNetworkInfo != null;
}

答案 1 :(得分:0)

假设你的循环返回正确的值,让我们取出当地的布尔值,因为它们是不必要的:

private boolean haveNetworkConnection() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    for (NetworkInfo ni : netInfo) {
        if (ni.getTypeName().equalsIgnoreCase("WIFI") && ni.isConnected())
            return true;
        if (ni.getTypeName().equalsIgnoreCase("MOBILE") && ni.isConnected())
            return true;
    }
    return false;
}

如果循环不正确,您可以在代码中加入the documentation's method

private boolean haveNetworkConnection() {
    ConnectivityManager cm =
            (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if(activeNetwork != null)
        return activeNetwork.isConnectedOrConnecting();
    return false;
}

答案 2 :(得分:0)

private boolean haveNetworkConnection() {
    //boolean haveConnectedWifi = false;
    //boolean haveConnectedMobile = false;

    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    for (NetworkInfo ni : netInfo) {
        if (ni.getTypeName().equalsIgnoreCase("WIFI"))
            if (ni.isConnected())
                return true;
        if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
            if (ni.isConnected())
                return true;
    }
    return false;
}

答案 3 :(得分:0)

您的逻辑错误如下:

if (haveNetworkConnection()) {
        if (loc != null && !loc.equals("")) {
            new GeocoderTask().execute(loc);
        } else {
            Toast.makeText(this, "Internet Connection is Required to view this map", Toast.LENGTH_LONG).show();
        }
    }

如果haveNetworkConnection()为false,则永远不会进入Toast。您应该按如下方式进行更改:

if (haveNetworkConnection()) {
            if (loc != null && !loc.equals("")) {
                new GeocoderTask().execute(loc);
            } 
        }
else {
                Toast.makeText(this, "Internet Connection is Required to view this map", Toast.LENGTH_LONG).show();
            }