【问题标题】:Verfiying Network Connection in Android [duplicate]在Android中验证网络连接[重复]
【发布时间】:2014-03-17 10:15:51
【问题描述】:

我想知道在发送数据之前如何在 Android 中验证网络连接? 谁能帮我 ? 我尝试了这段代码,但没有结果

public boolean isConnectingToInternet() {
    ConnectivityManager connectivity = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) 
    {
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null)
        { 
           for (int i = 0; i < info.length; i++) 
           {
               if (info[i].getState() == NetworkInfo.State.CONNECTED)
               {
                   return true;
               }
           }
        }
    }
    return false;
}

【问题讨论】:

  • 我会像this 问题中那样做.getActiveNetworkInfo().isConnected()。也是重复的。

标签: android connectivity


【解决方案1】:

检查你的清单:添加

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-09
    • 1970-01-01
    • 2012-09-05
    • 1970-01-01
    • 2013-04-06
    • 2014-08-12
    • 2020-10-13
    • 2021-01-31
    相关资源
    最近更新 更多