【问题标题】:How to check network connection enable or disable in WIFI and 3G(data plan) in mobile?如何在手机中检查 WIFI 和 3G(数据计划)中的网络连接启用或禁用?
【发布时间】:2011-07-19 10:30:51
【问题描述】:

我正在开发一个 android 应用程序,在我的应用程序中,我想检查网络连接,比如我想检查 wifi 和 3G 中的网络连接(就像印度人最喜欢手机中的数据计划),如何检查 wifi 中的网络和3G.不知道的,请给点意见。

谢谢

【问题讨论】:

    标签: android


    【解决方案1】:

    请试试这个

    public static boolean isInternetConnected (Context ctx) {
        ConnectivityManager connectivityMgr = (ConnectivityManager) ctx
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo wifi = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mobile = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        // Check if wifi or mobile network is available or not. If any of them is
        // available or connected then it will return true, otherwise false;
        if (wifi != null) {
            if (wifi.isConnected()) {
                return true;
            }
        }
        if (mobile != null) {
            if (mobile.isConnected()) {
                return true;
            }
        }
        return false;
    }
    

    请在 android manifest 文件中添加以下权限。

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    

    【讨论】:

    • 如果 SimCard 在平板电脑等设备中不可用,比.??
    • 连接wifi时返回true。但是当它连接到 wifi 但实际上没有数据获取用于浏览目的时。在这种情况下如何找到连接到wifi但没有互联网连接?
    【解决方案2】:

    这里是代码 sn-p。启用网络返回true,否则返回false

    private boolean netCheckin() {
        try {
            ConnectivityManager nInfo = (ConnectivityManager) getSystemService(
                Context.CONNECTIVITY_SERVICE);
            nInfo.getActiveNetworkInfo().isConnectedOrConnecting();
            Log.d(tag, "Net avail:"
                + nInfo.getActiveNetworkInfo().isConnectedOrConnecting());
            ConnectivityManager cm = (ConnectivityManager) getSystemService(
                Context.CONNECTIVITY_SERVICE);
            NetworkInfo netInfo = cm.getActiveNetworkInfo();
            if (netInfo != null && netInfo.isConnectedOrConnecting()) {
                Log.d(tag, "Network available:true");
                return true;
            } else {
                Log.d(tag, "Network available:false");
                return false;
            }
        } catch (Exception e) {
            return false;
        }
    }
    

    【讨论】:

    • 编辑代码 - 你有 2 个具有相同值的变量!你打电话给getActiveNetworkInfo()isConnectedOrConnecting() 3 次!
    • 上面的代码是正确的。但是,如果手机连接到 wifi 但实际上没有数据用于浏览怎么办。到时候怎么找。
    【解决方案3】:

    您可以使用以下适用于所有 API 版本的代码:

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

    【讨论】:

      【解决方案4】:

      我在 Activity 类中使​​用这个

      private boolean isConnectedToInternet() {
        ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        return (networkInfo != null);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-13
        • 1970-01-01
        • 1970-01-01
        • 2011-05-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-25
        相关资源
        最近更新 更多