13

I tried the below code to check whether my mobile is connected to a wireless network and it works well when I want to know if my mobile is connected to the network, but it fails to give information about the internet access ... something like "Pinging" any website. Actually I followed many links but still no answer, so I'll be so thankful if anybody can help.

Thanks in Advance.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Toast t = new Toast(getApplicationContext());

    if (isInternetOn()) {
         // INTERNET IS AVAILABLE, DO STUFF..
         Toast.makeText(ConnectivityTestActivity.this,"Network is Available", Toast.LENGTH_LONG).show();

         }
    else {
         // NO INTERNET AVAILABLE, DO STUFF..
        Toast.makeText(ConnectivityTestActivity.this,"No Network Available", Toast.LENGTH_LONG).show();

        }
}

public final boolean isInternetOn() {


    ConnectivityManager connec =  (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    // ARE WE CONNECTED TO THE NET
    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 ) {
    // MESSAGE TO SCREEN FOR TESTING (IF REQ)
    //Toast.makeText(this, connectionType + ” connected”, Toast.LENGTH_SHORT).show();
    return true;
    } else if ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED ||  connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED  ) {

    return false;
    }
    return false;
    }}

EDIT:

Follow below link it contains a great answer for Ping google server and get result

https://stackoverflow.com/a/16458623/1239911

Community
  • 1
  • 1
Amt87
  • 5,493
  • 4
  • 32
  • 52

3 Answers3

8

Actually I used the same function isInternetOn() but I removed the connecting condition. It had to check the status of connection if connected or not and if it is trying to connect. This didn't work for me, so I removed connecting status checking and then it worked.

Thanks for all replies.

public final boolean isInternetOn()
{
  ConnectivityManager connec = (ConnectivityManager)
    getSystemService(Context.CONNECTIVITY_SERVICE);

  // ARE WE CONNECTED TO THE NET
  if ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED ||
       connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED )
  {
    // MESSAGE TO SCREEN FOR TESTING (IF REQ)
    //Toast.makeText(this, connectionType + ” connected”, Toast.LENGTH_SHORT).show();
    return true;
  }
  else if ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED
    ||  connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED  )
  {
    return false;
  }

  return false;
}
dldnh
  • 8,923
  • 3
  • 40
  • 52
Amt87
  • 5,493
  • 4
  • 32
  • 52
  • 3
    Just a note ... I used the above code from another question here in stackoverflow.com, we may use 'ConnectivityManager.Type_WIFI' or 'ConnectivityManager.Type_Mobile' instead of 0 & 1 in the getNetworkInfo() – Amt87 Apr 02 '12 at 08:18
  • 1
    Why did you even need the `else if` part? If we are not conected that means we are disconnected. Can you please let me know if there is any use of this `else if` part? – Adil Malik Feb 08 '13 at 16:05
  • 1
    It might be CONNECTED, DISCONNECTED, CONNECTING, DISCONNECTING, SUSPENDED and UNKNOWN. You can check theses states in developer.android.com or when you click ctrl+space after (NetworkInfo.State.) in your code. I wish this can be helpful. – Amt87 Feb 10 '13 at 07:47
  • 1
    But in your case, if the `if` condition fails. The `else` condition will be evaluated, now wheather the `else` condition passes or fails in both cases we are returning `false`. Then why do we need `else` condition in above case? That was my point. Any ways Good Info in your last comment. – Adil Malik Feb 10 '13 at 17:47
  • I see :) . Actually I want it to return false only when it is DISCONNECTED – Amt87 Feb 11 '13 at 06:32
  • Here is a good ping answer http://stackoverflow.com/a/16458623/1239911 – Amt87 Mar 21 '15 at 08:31
6

see the sample:

public static boolean isWifiEnabled() {
    if ( !gWifiManager.isWifiEnabled()) {

        if (mCanShowWifiToast) {
            new Thread(mWifiToastControl).start();
            G.gHandler.post(mNoWifiRunnable);
        }

        return false;
    } else {
        int linkspeed = gWifiManager.getConnectionInfo().getLinkSpeed();
        if (linkspeed < 5) {
            if (mCanShowWifiToast) {
                new Thread(mWifiToastControl).start();
                G.gHandler.post(mNoWifiRunnable);
            }

            return false;
        }
    }

    return true;
}
Behnam
  • 2,212
  • 2
  • 22
  • 32
2

You can use BroadcastReceivers that will trigger when the connection status change: there is a similar question with an answer: Broadcast intents for bluetooth, wifi and ringer mode

Community
  • 1
  • 1
Vítor Marques
  • 349
  • 4
  • 11