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”.


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

<Button android:id="@+id/button"
        android:text="Check Internet Status"


Step 3:

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


public class ConnectionActivity extends Activity {

	Boolean internetPresent = false;

	ConnectionDetector cd;

	public void onCreate(Bundle savedInstanceState) {

		Button btnStatus = (Button) findViewById(;

		cd = new ConnectionDetector(getApplicationContext());

		btnStatus.setOnClickListener(new View.OnClickListener() {

			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

		// 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;



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 *