welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Thursday, August 23, 2012

How to check internet connectivity in android?

Here I would like share bit of useful information used in android. In Some scenario user accessing date through network access, for an example consuming web service uses android application.
In such cases before call any service we must make sure mobile have an internet access.For checking that process you can use the below code to check the option

Step 1:
Use the below code wherever you want to check the connection
private boolean checkConnection()
           ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);       
           NetworkInfo netInfo = cm.getActiveNetworkInfo();
           if (netInfo != null && netInfo.isConnectedOrConnecting()) {
           return true;

           return false;

This one return Boolean value with respect the process checking.
Step 2:

Also you need to add the below code get the access permission to check the internet connection in android manifest file.
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

That’s it no you have finished the process, now you can run the code check the function for checking internet connection.
Happy coding……..Post comments…


Post a Comment