2012-07-08

It is better to check internet connectivity status before making any HTTP Requests to avoid http exceptions. This tutorial will explain how to detect internet connection status in your applications.



Create new Project

1. Create a new project in Eclipse IDE by filling required details. File ⇒ New ⇒ Android Project

2. After creating new project first step is to add required permission in your AndroidManifest.xml file.

To access internet we need INTERNET Permission

To detect network status we need ACCESS_NETWORK_STATE Permission

3. Create a new Class file and name it as ConnectionDetector.java and type the following code.

4. When ever you want to check Internet Status in your application call isConnectingToInternet() function and it will return true or false

5. In this tutorial for testing purpose i placed a simple button. Once you press the button it will show appropriate alert message about internet connection status.

6. Open your main.xml under res/layout folder and create a button.

7. Finally open your MainActivity file and paste the following code. In the following code i showed an alert dialog with respected internet status message.

Run and test your application. For the above you will be getting following outputs.



Show more