How to check Internet Connectivity in Android Example

internet_feature_androidarena

Hello guys ,how are today, today’s topic is very much easy and quick , in this tutorial we are going to do how to check the internet connectivity on Android device programatically .You can check it and include your code within if , else loop and do your desire codings.
Lets have a look on coding part – First of all i take one button to test internet onClick of Button.You can Download Source code-

Download Code

activity_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/btn_check"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Click to check internet connectivity"
        android:layout_centerInParent="true"/>

</RelativeLayout>

 MainActivity.java

package androidarena.androidinternetcheck;


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


public class MainActivity extends Activity {

		Boolean isConnectionExist = false;
		InternetDetector cd;

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

			Button btnStatus = (Button) findViewById(R.id.btn_check);
			cd = new InternetDetector(getApplicationContext());
			btnStatus.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					isConnectionExist = cd.checkMobileInternetConn();
					if (isConnectionExist) {
						showAlertDialog(MainActivity.this, "Internet Connection",
								"Yeah ! Internet Found !!", true);
					} else {
					
						showAlertDialog(MainActivity.this, "No Internet Connection",
								"Your device doesn't have mobile internet", false);
					}
				}

			});

		}

		@SuppressWarnings("deprecation")
		public void showAlertDialog(Context context, String title, String message, Boolean status) {
			AlertDialog alertDialog = new AlertDialog.Builder(context).create();
			alertDialog.setTitle(title);
			alertDialog.setMessage(message);
			alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);

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

InternetDetector.java

package androidarena.androidinternetcheck;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class InternetDetector {
	private Context mcontext;

	public InternetDetector(Context context) {
		this.mcontext = context;
	}

	public boolean checkMobileInternetConn() {
		ConnectivityManager connectivity = (ConnectivityManager) mcontext
				.getSystemService(Context.CONNECTIVITY_SERVICE);
	
		if (connectivity != null) {
			NetworkInfo info = connectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
			
			if (info != null) {
				if (info.isConnected()) {
					return true;
				}
			}
		}
		return false;
	}
}

You can download PNG images or icons from the source code in drawable folder .If you want to customize your dialog box .

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="androidarena.androidinternetcheck"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
    
 <uses-permission android:name="android.permission.INTERNET" />
	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
	
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 

Now Run your Project and check out the output-

OutPut-

androidarena_internet

 

internet2

 

internet

 

If you have any query related to this code and any Topic regarding Android you can ask me any time 🙂 Happy coding.

Leave a Reply

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