Question

I want to know how to verify the network connection in Android before sending data ? Can anyone help me ? I try this code but no results

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;
}
Was it helpful?

Solution

check your manifest :add

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top