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.
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;
}
}
11 comments:
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.
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?
I tested this its not working....!
add this permission in androidmanifest.xml
now it work!
uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"
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
Great code.....Worked fine
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 .
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?
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..
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