2

Possible Duplicate:
Android - detect whether there is an Internet connection available

this is the code to check the internet availability on android device:

public boolean isInternetAvailable() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm.getActiveNetworkInfo() != null)
        return cm.getActiveNetworkInfo().isConnected();
    else
        return false;
}

So this code is working correctly if no wifi or network available.

But if device is connected to wifi network but internet is not available then what should i do to check the internet is there or not?

any suggestion will be appreciated.

Community
  • 1
  • 1
Rushabh Patel
  • 3,052
  • 4
  • 26
  • 58
  • You could use a BroadCastReceiver which will get broadcasted when connection is gone. – Braj Aug 20 '12 at 09:40
  • You could try and ping some address. – nullpotent Aug 20 '12 at 09:41
  • 1
    If you want to this for your Http request for server then instead of checking internet connection I think set Connection timeout is the best option. Look at http://stackoverflow.com/questions/11096987/check-real-internet-connection/11097184#11097184 – user370305 Aug 20 '12 at 09:42
  • 1
    Have a look at http://stackoverflow.com/questions/10350449/how-to-check-the-internet-connection-periodically-in-whole-application/10350511#10350511][1] – Braj Aug 20 '12 at 09:50

3 Answers3

2

I do it like this:

I try to reach google.com and watch for response:

public static void isGoogleAvailable(final Handler handler)
{
    new Thread()
    {
        private boolean hasGoogleResponded = false;
        @Override
        public void run()
        {
            new Thread()
            {
                @Override
                public void run()
                {
                    HttpGet requestForTest = new HttpGet("https://www.google.com/");
                    try
                    {
                        new DefaultHttpClient().execute(requestForTest);
                        hasGoogleResponded = true;
                    }
                    catch (Exception e)
                    {}
                }
            }.start();

            try
            {
                int waited = 0;
                while(!hasGoogleResponded && (waited < 60000))
                {
                    sleep(100);
                    if(!hasGoogleResponded )
                    {
                        waited += 100;
                    }
                }
            }
            catch(InterruptedException e)
            {}
            finally
            {
                if (hasGoogleResponded)
                {
                    handler.sendEmptyMessage(1);
                }
                else
                {
                    handler.sendEmptyMessage(0);
                }
            }
        }
    }.start();
}

Then I retrive messages in my handler:

Handler internetHandler = new Handler()
{
    @Override
    public void handleMessage(Message msg)
    {
        if (msg.what == 1) //connected
        {

        }
        else //not connected
        {

        }
    }
};

I hope this helps.

Repminister
  • 156
  • 1
  • 3
  • 6
1

You could try to ping a server like google (that should always exist) see this topic

Community
  • 1
  • 1
Laucia
  • 222
  • 2
  • 11
-1

You can try

 ConnectivityManager m;
            m.getAllNetworkInfo();

First check internet connection,then others. But sometimes its depends on app architecture. If application needed wifi and you havent - you can not check internet because it is not necessary

Yahor10
  • 2,123
  • 1
  • 13
  • 13