Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Verfiying Network Connection in Android [duplicate]

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;
}
like image 346
ZACK Avatar asked Nov 10 '22 12:11

ZACK


1 Answers

check your manifest :add

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
like image 111
Zied R. Avatar answered Nov 14 '22 23:11

Zied R.