Ullas
Ullas

Reputation: 383

Android internet connection code

I am developing an application. The scenario is: my Android device is showing connection to internet but the device doesn't have internet access. In my application I am using this code to check the internet connection:

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager 
          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}


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

But every time it crashes on the scenario I explained above. Any help would be appreciated.

Thanks

Upvotes: 1

Views: 956

Answers (4)

JMontero
JMontero

Reputation: 1865

Check this:

private boolean isNetworkAvailable()
{
  ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  State mobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
  State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();

  if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING)
  {             
      // Wifi connected     
  }
  else if(mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING)
  {
      // Mobile network connected
  }
}

Upvotes: 0

Sunil Kumar
Sunil Kumar

Reputation: 7082

use this

public static boolean isInternetOn(Context context) {

    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    // test for connection
    if (cm.getActiveNetworkInfo() != null
            && cm.getActiveNetworkInfo().isAvailable()
            && cm.getActiveNetworkInfo().isConnected()) {
        Log.v(TAG, "Internet is working");
        // txt_status.setText("Internet is working");
        return true;
    } else {
        // txt_status.setText("Internet Connection Not Present");
        Log.v(TAG, "Internet Connection Not Present");
        return false;
    }
}

Upvotes: 0

Rachit Tyagi
Rachit Tyagi

Reputation: 678

I wrote this method to handle this

public boolean isOnline(Context con) {
        try {
            connectivityManager = (ConnectivityManager) con
                        .getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        connected = networkInfo != null && networkInfo.isAvailable() &&
                networkInfo.isConnected();
        return connected;


        } catch (Exception e) {
            System.out.println("CheckConnectivity Exception: " + e.getMessage());
            Log.v("connectivity", e.toString());
        }
        return connected;
    }

and also add these permissions to manifest file

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

Upvotes: 4

Abx
Abx

Reputation: 2882

Try this code

public boolean isConnected(){
        ConnectivityManager connectivity = (ConnectivityManager) _context.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;
    }

Upvotes: 0

Related Questions