【问题标题】:How to check programmatically that wifi is connected or data pack is enabled but there is no internet connection?如何以编程方式检查 wifi 是否已连接或数据包已启用但没有互联网连接?
【发布时间】:2018-01-03 12:48:31
【问题描述】:

我正在使用 wifi 和 GPRS 连接互联网。但在某些情况下,例如 wifi 连接但没有互联网连接和数据 (GPRS) 相同。数据打开但没有网络连接。

对于 wifi 我使用下面的代码。这总是返回 true

 public static boolean isConnectedWifi(Context context) {
        NetworkInfo info=null;
        if(context!=null){
            info= IsNetConnectionAvailable.getNetworkInfo(context);
        }
        return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI);
    }

对于移动数据,我使用以下代码。它会根据数据给出真假。

public boolean isMobileDataEnable() {
    boolean mobileDataEnabled = false; // Assume disabled
    ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
    try {
        Class cmClass = Class.forName(cm.getClass().getName());
        Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
        method.setAccessible(true); // Make the method callable
        // get the setting for "mobile data"
        mobileDataEnabled = (Boolean) method.invoke(cm);
    } catch (Exception e) {
        // Some problem accessible private API and do whatever error handling you want here
    }
    return mobileDataEnabled;
}

但是在两种情况下都没有互联网连接时如何捕捉这种情况?是否有任何android api可以帮助解决上述情况。 请帮我解决这个问题。

【问题讨论】:

  • 对于一个网站,我会做一个简单的 XHR 请求来确定我是否可以连接到网络,也许你也可以这样做?
  • @mic 是的,我这样做了,但这很耗时。因为我应该在每个活动中检查网络连接

标签: android android-wifi gprs


【解决方案1】:

获取网络类型的方法是移动还是wifi。

public static String getNetworkType(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getActiveNetworkInfo();
    return info.getTypeName();
}

判断手机是否连接到互联网的方法

public static boolean isInternetIsConnected(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (activeNetwork != null) { // connected to the internet
        if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
            // connected to wifi
            return true;

        } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
            // connected to the mobile provider's data plan
            return true;
        }
    } else {
        // not connected to the internet
        return false;
    }
    return false;
}

您可以使用下面的library 来更好地控制网络连接。

【讨论】:

  • 感谢您的回答.. 但我的问题不同。您的方法 isInternetIsConnected() 在连接到数据或 wifi 时始终为 true。但是我的问题是,在这两种情况下,wifi 或某些数据计划问题(数据溢出)或信号波动时,您的方法都是正确的,但实际上没有网络连接。我想抓住这些条件并告诉用户没有可用的网络。我期待 android API 帮助
  • 你有没有看过它有观察功能的库。
  • 这里还有一个查看网速的链接android.jlelse.eu/…
  • ReactiveNetwork 能够解决我的问题,但它让我的应用程序太慢了......使用这个库的任何替代解决方案
【解决方案2】:

您可以使用此Facebook Connection Class 查找网络速度以及您是否已连接到网络。对于每个活动,您无需检查只需创建一个应用程序类并实现逻辑并通过该类获取连接检查器。

无论我是否有一个具有指定带宽范围的活动网络连接,我都已为实时流式传输实现此功能。我没有适用于您的方案的工作代码。这可能会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-06
    • 1970-01-01
    • 2017-04-28
    • 2020-12-09
    • 2018-02-02
    • 2018-09-09
    相关资源
    最近更新 更多