RSS

Search Engine

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:

Mark said...

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.

Anonymous said...

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?

Anonymous said...

I tested this its not working....!

Anonymous said...

add this permission in androidmanifest.xml



now it work!

Anonymous said...

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

Anonymous said...

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

Anonymous said...

Great code.....Worked fine

Anonymous said...

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 .

Anonymous said...

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?

ullas ek said...

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..

Meghna said...

Thanks for the nice tutorial man... Even this
http://www.compiletimeerror.com/2013/06/check-internet-connection-in-android.html
might help, have a look...

Post a Comment