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.

ConnectivityManager cm = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);

2. Next, access which type of network exists and save the corresponding value into a variable. Do this by typing in the following code.

NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); if (null != activeNetwork) { if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) answer="You are connected to a WiFi Network"; if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) answer="You are connected to a Mobile Network"; } else answer = "No internet Connectivity";

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.

Toast.makeText(getApplicationContext(), answer, Toast.LENGTH_LONG).show();

4. Well, that’s all for the Java part. This is the complete Main Activity Java file for my project. Make sure you have all these classes imported into your class file.

package com.slashcoding.internetconnect; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.view.Menu; import android.widget.Toast; public class MainActivity extends Activity { String answer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ConnectivityManager cm = (ConnectivityManager) getApplicationContext() .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); if (null != activeNetwork) { if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) answer="You are connected to a WiFi Network"; if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) answer="You are connected to a Mobile Network"; } else answer = "No internet Connectivity"; Toast.makeText(getApplicationContext(), answer, Toast.LENGTH_LONG).show(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }

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.

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

That’s all. You just learned how to check whether an active internet connection exists in the android device or no. Performing this check is a way of validation in the program. It prevents the application from crashing if no internet connection is available. At Slash Coding, I will be sharing various other posts regarding Android Development and much more! Don’t forget to subscribe to Slash Coding for latest post updates via RSS Feeds, Facebook, Google+ or Twitter.