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.

4
  • You could use a BroadCastReceiver which will get broadcasted when connection is gone. Commented Aug 20, 2012 at 9:40
  • You could try and ping some address. Commented Aug 20, 2012 at 9: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 stackoverflow.com/questions/11096987/… Commented Aug 20, 2012 at 9:42
  • 1
    Have a look at stackoverflow.com/questions/10350449/… Commented Aug 20, 2012 at 9:50

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

A thread in a thread? Sorry but this is an awful implementation to check that...
1

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

Comments

-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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.