Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How to check internet Access, not just connectivity to wifi? [duplicate]

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

like image 368
Amt87 Avatar asked Mar 25 '12 15:03

Amt87


2 Answers

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;
}
like image 165
Amt87 Avatar answered Oct 14 '22 19:10

Amt87


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;
}
like image 28
Behnam Avatar answered Oct 14 '22 19:10

Behnam