Android Detect Internet Connection Status

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.

Download Code

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

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

        android:label="@string/app_name" >
            android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
    <!-- Internet Permissions -->
    <uses-permission android:name="android.permission.INTERNET" />

    <!-- Network State Permissions -->
	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


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

package com.example.detectinternetconnection;

import android.content.Context;

public class ConnectionDetector {
	private Context _context;
	public ConnectionDetector(Context context){
		this._context = context;

	public boolean isConnectingToInternet(){
		ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
		  if (connectivity != null) 
			  NetworkInfo[] info = connectivity.getAllNetworkInfo();
			  if (info != null) 
				  for (int i = 0; i < info.length; i++) 
					  if (info[i].getState() == NetworkInfo.State.CONNECTED)
						  return true;

		  return false;

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

ConnectionDetector cd = new ConnectionDetector(getApplicationContext());

Boolean isInternetPresent = cd.isConnectingToInternet(); // 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.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""
    android:orientation="vertical" >

        android:text="Detect Internet Status" />
    <Button android:id="@+id/btn_check"
        android:text="Check Internet Status"


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.

package com.example.detectinternetconnection;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class AndroidDetectInternetConnectionActivity extends Activity {

	// flag for Internet connection status
	Boolean isInternetPresent = false;
	// Connection detector class
	ConnectionDetector cd;

	public void onCreate(Bundle savedInstanceState) {

		Button btnStatus = (Button) findViewById(;

		// creating connection detector class instance
		cd = new ConnectionDetector(getApplicationContext());

		 * Check Internet status button click event
		 * */
		btnStatus.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// get Internet status
				isInternetPresent = cd.isConnectingToInternet();

				// check for Internet status
				if (isInternetPresent) {
					// Internet Connection is Present
					// make HTTP requests
					showAlertDialog(AndroidDetectInternetConnectionActivity.this, "Internet Connection",
							"You have internet connection", true);
				} else {
					// Internet connection is not present
					// Ask user to connect to Internet
					showAlertDialog(AndroidDetectInternetConnectionActivity.this, "No Internet Connection",
							"You don't have internet connection.", false);



	 * Function to display simple Alert Dialog
	 * @param context - application context
	 * @param title - alert dialog title
	 * @param message - alert message
	 * @param status - success/failure (used to set icon)
	 * */
	public void showAlertDialog(Context context, String title, String message, Boolean status) {
		AlertDialog alertDialog = new AlertDialog.Builder(context).create();

		// Setting Dialog Title

		// Setting Dialog Message
		// Setting alert dialog icon
		alertDialog.setIcon((status) ? R.drawable.success :;

		// Setting OK Button
		alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {

		// Showing Alert Message;

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

android test internet connectivity
android test internet connectivity no intenet
android test internet connectivity

Subscribe to our Newsletter!

Join our 746,498 subscribers and get instant access to the latest android tutorials, app reviews and much more!