Slash Coding

  • Android Development
  • CSS
  • Subscribe
  • About Us
  • Contact Us
You are here: Home / Android Development / Checking Active Internet Connection in Android Application

Checking Active Internet Connection in Android Application

By Aneesh Bhatnagar

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.

Android Internet Connectivity

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.

  • Creating your First Android Application
  • The Basic Elements of an Android Application

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.

Android Internet Project 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" />

Android Internet Connectivity

 

Download Source Code

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.

Did you enjoy this article?
Subscribe to our email alerts when we post something new so you don't miss out.

About Aneesh Bhatnagar

Aneesh Bhatnagar is a freelance web developer, who specializes in front-end website development with HTML5, CSS3 and jQuery. He believes in transforming the entire web into a Responsive Web. He also specializes in use of Bootstrap framework to get websites up and running.

Comments

  1. kesavan.muthuvel says

    March 23, 2014 at 9:56 am

    good and simple

    • Aneesh Bhatnagar says

      March 23, 2014 at 9:58 am

      Thank you for your comment! 🙂

  2. Pup says

    February 26, 2015 at 12:31 pm

    The simplest one ! and the effective one!!

    • Aneesh Bhatnagar says

      March 4, 2015 at 6:02 pm

      I’m glad this tutorial could help you! 🙂

  3. vanitha says

    March 12, 2015 at 4:04 pm

    need help for runniing Android password store source code

    • Aneesh Bhatnagar says

      March 12, 2015 at 4:12 pm

      Hey Vanitha!

      I’m unaware of any such source code. If you got it from any other website, it’s better to get in touch with the author there and have your problem resolved! 🙂

  4. deni says

    September 14, 2016 at 8:43 pm

    Great! Very helpful. Thanks!

  5. arjun says

    January 11, 2017 at 5:14 am

    PLEASE HELP ME RESOLVE A PROBLEM I’m SEARCHING FOR IT FROM LAST 15 DAYS……YOUR CODE ONLY TELLS NETWORK CONNECTIVITY BUT NOT INTERNET CONNECTIVITY….IF A WIFI DO NOT HAVE INTERNET CONNECTIVITY THEN HOW TO CHECK IT….

Search Slash Coding

Follow Us

RSS Feeds Facebook Twitter Follow Google+

Categories

  • Android Development
  • C++
  • Developer Tips
  • Slash Coding
  • Web Development
    • CSS
    • JavaScript
    • jQuery
    • PHP

Recently Published

  • Moving Ahead from Front End and Android Development
  • How to Export a Project from Android Studio
  • What is jQuery? Let’s Answer this Common Question today!
  • Mobile App Prototyping : Pixate Studio
  • How to Create a Countdown Timer using jQuery

Subscribe to Email Alerts

Eager to learn more from Slash Coding?
Sign up for daily email updates when anything new comes up. Get it straight in your inbox.

Follow Us on Social Media

RSS Feeds Facebook Twitter Follow Google+

Copyright © 2025 · Slash Coding · Powered by the Genesis Framework
Sitemap | Privacy Policy | About | Contact

×
Get Notified of the new Tutorials when they are posted!
Never miss any article update from Slash Coding. Subscribe to email alerts today!