Checking Active Internet Connection in Android Application

Checking Active Internet Connection in Android Application

Android applications are getting more and more popular these days. A lot more programmers and developers are getting together to build Android Applications for the masses. Also, not to forget that most of these Android Applications need to access the internet for one or the other thing. Though almost every Android phone out there is generally connected to the internet, there may be a case when there is no internet connectivity on the phone. We can not be certain that the internet connection exists.

To make sure that the internet connection exists, Android SDK allows you to check the Network status by making use of the Connection Manager Class in Android. Today, in this tutorial, I am going to teach you how to check if an active internet connection exists or no. If it exists, is it Cellular data or WiFi Access. But before we proceed ahead, make sure that you are aware with creating Android applications and the basics of android application. If you are not, head over to these posts and read them.

Let us begin by creating a new project and implement this technique. I hope you know how to do that by now. Still for your reference I created the application with the following settings. You may use these settings.

Access Network Status in Android

Now, let’s get started writing down the basic code we want for the application. I will show you how to check internet connectivity and we will make a toast on the screen showing which type of connectivity exists. You can then implement it on your actual project easily.

1. Since we don’t need any specific elements in the layout, I will not make any changes to the layout.xml file. Let’s head over to the MainActivity Java class. Open the class and in the OnCreate method, create a new instance of the ConnectionManager class by typing in the following line of code.

3. Next, based on the value saved in this variable, we will create a toast saying what type of a connection is available. Note that this is the actual place where you need to put in your commands to access the internet in your actual application.

5. Now, head over to the AndroidManifest.xml file and add the following line of code just after the opening tags. This will allow the application to have adequate access to check the availability of Internet on the device.