在Android中验证网络连接

ZAC*_*ACK 5 android connectivity

我想知道如何在发送数据之前验证 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;
}
Run Code Online (Sandbox Code Playgroud)

Zie*_*bhi 1

检查你的清单:添加

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Run Code Online (Sandbox Code Playgroud)