How to Check Internet Connectivity in Android using ConnectivityManager

Hello Friends,

In this blog post i am going brief about, how to check Internet Connection using ConnectivityManager class in Android.

Use of ConnectivityManager
ConnectivityManager is class, that answers queries about the state of network connectivity.

Step 1:
First, you have to user permission in Android Manifset file for Accessing Internet and Accessing Network State. For that include the following two lines in your Manifest file:

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

Step 2:
Include the following Xml file in your “Layout Folder”.

main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    
<Button android:id="@+id/button"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Check Internet Status"
        android:layout_centerInParent="true"/>

</RelativeLayout>

Step 3:

Include the following java class in your “Source Folder”:

ConnectionActivity


public class ConnectionActivity extends Activity {

	Boolean internetPresent = false;

	ConnectionDetector cd;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		Button btnStatus = (Button) findViewById(R.id.button);

		cd = new ConnectionDetector(getApplicationContext());

		
		btnStatus.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				
				// get Internet status
				internetPresent = internetkStatus();

				// check for Internet status
				if (internetPresent) {
					// 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);
				}
			}

		});

	}
	
	public boolean internetkStatus(){
		ConnectivityManager connectivity = (ConnectivityManager) this.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;
	}


	public void showAlertDialog(Context context, String title, String message, Boolean status) {
		AlertDialog alertDialog = new AlertDialog.Builder(context).create();

		// Setting Dialog Title
		alertDialog.setTitle(title);

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

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

		// Showing Alert Message
		alertDialog.show();
	}
}

Screenshots

Screenshot_2014-12-21-12-48-25Screenshot_2014-12-21-12-48-31Screenshot_2014-12-21-12-48-44

Hope this blog post was helpful. Do let me know your feedback.

Leave a Reply

Your email address will not be published. Required fields are marked *