Check internet connection in android programmatically

Detect all available internet connection 2G,3G,4G,5G,WiFi.

Android application developer can manually check internet connection is on off status using programming method. This feature called as Check internet connection in android programmatically.Using this method builder can detect data connections with the use of MainActivity.java file.

android-project-download-code-button

Note : Before start checking internet connection status add network configuration permissions file into AndroidManifest.xml file So i am putting here whole files code.

How to Check internet connection in android programmatically.

Code for AndroidManifest.xml file.

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.android_examples.com.checkinternetconnection"
 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" />
 <uses-permission android:name="android.permission.CHANGE_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>

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.android_examples.com.checkinternetconnection.MainActivity" >

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="158dp"
 android:text="CLICK HERE TO CHECK INTERNET CONNECTION STATUS" />

</RelativeLayout>

Code for MainActivity.java file.

package com.android_examples.com.checkinternetconnection;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

 Button btnNetworkcheck;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 btnNetworkcheck = (Button)findViewById(R.id.button1);
 
 
 btnNetworkcheck.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 ConnectivityManager ConnectionManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
 NetworkInfo networkInfo=ConnectionManager.getActiveNetworkInfo();
 if(networkInfo != null && networkInfo.isConnected()==true )
 {
 Toast.makeText(MainActivity.this, "Network Available", Toast.LENGTH_LONG).show();
 
 }
 else
 {
 Toast.makeText(MainActivity.this, "Network Not Available", Toast.LENGTH_LONG).show();
 
 }
 
 }
 });
 }
}

Screenshots:

before start internet connection

Check internet connection in android programmatically

Click Here To Download Check internet connection in android programmatically project.

Leave a Reply

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