Monday, January 10, 2011

Check Internet Connection in android

Hello Friends,

Today discussed about how to check internet connection in android... This is very simple code implement..

First In androidmanifest.xml file give the permission of internet given below.

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

And Internet Connection code given below. This are simply create one checkinternetconnection() function.

So this code given below.

Code ::::::::::

private boolean checkInternetConnection() {

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

// ARE WE CONNECTED TO THE NET

if (conMgr.getActiveNetworkInfo() != null

&& conMgr.getActiveNetworkInfo().isAvailable()

&& conMgr.getActiveNetworkInfo().isConnected()) {

return true;

} else {

Log.v(TAG, "Internet Connection Not Present");

return false;

}

}

Enjoyed.

11 comments:

  1. Thanks for the code - does this check whether the internet is actually working, for instance if the user is logged into a public wifi and they haven't yet authenticated. In that case - all requests return the wifi login page.

    ReplyDelete
  2. Hmm, haven't tested it, but I don't think that it works on public authention-required networks, considering that conMgr.getActiveNetworkInfo().isConnected() should return true as soon as you're connected to the network - without you having never visited the authentication page through the web.
    This is indeed a problem if you directly try to connect to a external DB etc..

    Hmm, what happens if you're connected to the internet through the Mobile Network? Then you technically aren't connected to a network (router?)? - won't this be a problem aswell?

    ReplyDelete
  3. I tested this its not working....!

    ReplyDelete
  4. add this permission in androidmanifest.xml



    now it work!

    ReplyDelete
  5. uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"

    ReplyDelete
  6. Hello,
    I am New to android and I would like to list the nearest wifi connection in List view of my application. then i have to select the appropriate wifi and type the password... pls help me to code this app

    ReplyDelete
  7. Great code.....Worked fine

    ReplyDelete
  8. Note :just in case some ppl might not know .if you are testing on emulator connect and disconnect internet using " F8 " and not by disconnecting your actual internet .

    ReplyDelete
  9. for e.q.
    There is a wireless router but it does not have an access internet. If I connect to that router this code still returns true, however it should not return true,there is no internet connectivity.
    right?

    ReplyDelete
  10. Sir
    I tried this code in my Gps application..i want to inlude this code in my login page..how this possible..i tried this code but not getting result..where i have to put this code..please help me..

    ReplyDelete

How to Take a Screenshot on Android (All Methods)

Taking a screenshot on Android should be simple, and most of the time it is - but the exact method varies more than people expect depending ...