0

我正在尝试检测设备是否具有 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在可以访问 Internet 时显示,但在没有 Internet 访问时不显示 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();
    }
}
4

4 回答 4

0

Try this to check whether an internet connection is available:

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

    return activeNetworkInfo != null;
}
于 2012-12-01T18:17:18.220 回答
0

Assuming your loop returns the correct values, let's take out the local booleans since they are unnecessary:

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

If the loop is incorrect, you can incorporate the documentation's method in your code:

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

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if(activeNetwork != null)
        return activeNetwork.isConnectedOrConnecting();
    return false;
}
于 2012-12-01T18:17:29.857 回答
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;
}
于 2012-12-01T18:21:41.913 回答
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 () 为假,您将永远无法到达 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();
            }
于 2012-12-01T18:37:13.730 回答