0

I'm interested in how Android checks internet connectivity. For example in our company's wifi the wlan-symbol turns grey, even if there is working connection for http/https/dns etc. At Home (No restrictions on Firewall) it turns blue.

Which checks are done in background? Does anybody know it in detail?

redflag237
  • 222
  • 4
  • 16
  • First Search!Check here : http://stackoverflow.com/questions/1783117/network-listener-android – Arash GM Feb 18 '13 at 12:42
  • Guys my question was no code matter. It was a technical question, which is done by android to determine an connection as up or down – redflag237 Feb 18 '13 at 12:50
  • 1
    that question is probably more suitable to http://android.stackexchange.com/ then here. SO is for programming questions. Anyway, to be absolutely sure you can check the source code, it's there. But if I had to guess I would say that they ping some google server to test connection. – Budius Feb 18 '13 at 13:30

3 Answers3

0
public final boolean isInternetOn() {

    ConnectivityManager connec =  (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

    if ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED ||
            connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTING ||
            connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING ||
            connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED ) 
    {
        return true;
    } else if ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED ||  
            connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED  ) 
    {

        return false;
    }
    return false;
}//end of the Internet Connection Checking
Kanaiya Katarmal
  • 5,974
  • 4
  • 30
  • 56
0
private boolean isNetworkConnected() {
   ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
     NetworkInfo ni = cm.getActiveNetworkInfo();
  if (ni == null) {
  // There are no active networks.

return false; } else return true; }

       <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Rohit
  • 3,401
  • 4
  • 33
  • 60
  • 1
    This is what i found on related posts, too. But what is Android doing exactly? DNS resolve or any HTTP request? – redflag237 Feb 18 '13 at 12:44
  • what is android doing to determine if the connection is up or down? In this case, not by code. – redflag237 Feb 18 '13 at 12:46
  • try this NetworkInfo i = conMgr.getActiveNetworkInfo(); if (i == null) return false; if (!i.isConnected()) return false; if (!i.isAvailable()) return false; return true; – Rohit Feb 18 '13 at 12:51
0

you can use this function to check network in your app:

  public boolean isConnectToInternet() {
        ConnectivityManager connectivity = (ConnectivityManager) 
        getApplicationContext().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 you must add this permission at manifest.xml:

  <uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Milad Mohamadi
  • 127
  • 1
  • 10