Alireza Peer
Alireza Peer

Reputation: 908

Android - Check Internet Access

I have read this Answer About Getting Internet Connection Status in Android:

https://stackoverflow.com/a/22256277/4225644

But It doesn't work properly, for example if i have a network connection with no internet Access, this method takes too long time to return False:

public Boolean isOnline() {
try {
    Process p1 = java.lang.Runtime.getRuntime().exec("ping -c 1 www.google.com");
    int returnVal = p1.waitFor();
    boolean reachable = (returnVal==0);
    return reachable;
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
return false;
}

How Can I decrease this Time to have a faster answer?

Upvotes: 2

Views: 142

Answers (3)

Ahmad Aghazadeh
Ahmad Aghazadeh

Reputation: 17131

Use this code :

public static boolean isInternetConnected(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo ni = cm.getActiveNetworkInfo();

    if (ni == null)
        return false;
    else {
        if (ni.isConnected())
            if (isOnline(context))
                return true;
            else
                return false;
        return false;
    }
}


public static boolean isOnline(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnected()) {
        try {
            URL url = new URL("http://www.google.com");
            HttpURLConnection urlc = (HttpURLConnection) url
                    .openConnection();
            urlc.setConnectTimeout(2000);
            urlc.connect();
            if (urlc.getResponseCode() == 200) {
                return new Boolean(true);
            }
        } catch (MalformedURLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return false;
}

Upvotes: 1

Amit Basliyal
Amit Basliyal

Reputation: 870

this code may be help you if you want to check internet is present or not

public boolean isConnectingToInternet(){
  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;
 }

and give permission in your manifest.xml

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

Upvotes: 0

fiipi
fiipi

Reputation: 663

You could check if there's connectivity with

ConnectivityManager cm =
        (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
                      activeNetwork.isConnectedOrConnecting();

That's for sure a faster way to determine if there's network acces instead of performing a request and waiting for a failure.

See docs as well.

Upvotes: 0

Related Questions