How to check the internet connection in android

Android provides the features by which you can check the internet connection.

In my prgram run the app it will show you the buttn check internet connection when you click on the button it will give the message connected if you are connected with internet otherwise it will give the message not connected.


Internet Connection Checker:.

In this section i am going to show you how to check the internet connection with practical example.

First open the android studio and create a project and give the name of your project like ConnectivityDemo.

The screenshots of the program is as follows:

media image media image

You need to drag and drop the one Button from pallet. Open the activity_main.xml file and code look like this


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

<Button
        android:id="@+id/btncheck"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_red_dark"
        android:textColor="@android:color/white"
        android:layout_marginBottom="1dp"
        android:text="Check Internet Connection"
        />

</RelativeLayout>
       

Open the AndroidManifest.xml file and give the permission for internet.The code is look like this

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.niitpc.myapplication">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
   

<application
   android:allowBackup="true"
   android:icon="@mipmap/ic_launcher"
   android:label="@string/app_name"
   android:roundIcon="@mipmap/ic_launcher_round"
   android:supportsRtl="true"
   android:theme="@style/AppTheme">
   <activity android:name=".MainActivity">
   <intent-filter>
   <action android:name="android.intent.action.MAIN" />
   <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>
   </activity>
   </application>

</manifest>
       

In AndroidManifest.xml file only write two line of code for permission.

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

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


Open the java folder and right click on package of MainActivity class and a dialog box will display click on new->java class and give the name of class as ConnectionChecker

Open the ConnectionChecker class and write code look like this

package com.example.niitpc.myapplication;
import android.app.Service;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class ConnectionChecker {

Context context;

public ConnectionChecker(Context context)
{
 this.context=context;
}
public  boolean isConnect()
{
ConnectivityManager connectivityManager=(ConnectivityManager)context.getSystemService( Service.CONNECTIVITY_SERVICE);
if(connectivityManager!=null)
{
NetworkInfo networkInfo=connectivityManager.getActiveNetworkInfo();
if(networkInfo!=null)
{
 if(networkInfo.getState()==NetworkInfo.State.CONNECTED)
 {
    return true;
  }
 }
}
return  false;

}
}


Explanation:

Context context:Here we have declared the object context of Context.

public ConnectionChecker(Context context):Here i have created the constructor of ConnectionChecker class and pass the one argument as context object of the Context.

this.context=context:Here i have the context object to local class context object.

public boolean isConnect():Here i have created the isConnect() and their return type is boolean means true or false.

ConnectivityManager connectivityManager=(ConnectivityManager)context.getSystemService( Service.CONNECTIVITY_SERVICE):Here i have called the getSystemService() that take the one argument Service.CONNECTIVITY_SERVICE and this method returns the ConnectivityManager object.

if(connectivityManager!=null):Here i have check the connectivityManager object not equal to null then.

NetworkInfo networkInfo=connectivityManager.getActiveNetworkInfo():Here i have called the getActiveNetworkInfo() and it will return the NetworkInfo object.

``

if(networkInfo!=null):Here i have checked the networkInfo object not equal to null then.

if(networkInfo.getState()==NetworkInfo.State.CONNECTED):Here i have called the getState() with the help of networkInfo object and compare with NetWorkInfo.State.CONNECTED if both are same then it will return the true other wise false to isConnected().

imageView.setImageBitmap( bitmap ):The setImageBitmap() take the bitmap object as argument and set into imageView object.


Open the MainActivity java class and write code look like this.


public class MainActivity extends AppCompatActivity   {

Button btncheck;
ConnectionChecker checker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView(R.layout.activity_main );
    
btncheck=(Button)findViewById( R.id.btncheck );
checker=new ConnectionChecker( this );
btncheck.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
if(checker.isConnect())
{

 Toast.makeText( MainActivity.this,"Connected",Toast.LENGTH_LONG ).show();

}
else
{

 Toast.makeText( MainActivity.this,"not Connected",Toast.LENGTH_LONG ).show();

}
}
}
);

}
}



Explanation:

Button btncheck:Here we have declared the object btncheck of Button.

ConnectionChecker checker:Here i have declared the object checker of ConnectionChecker class.

btncheck=(Button)findViewById( R.id.btncheck ):Here i have initialized the btncheck object.

checker=new ConnectionChecker( this ):Here i have initialized the checker object.

btncheck.setOnClickListener:Here i have created the listener for btncheck object of Button.

public void onClick(View v):Here i have override the onClick().

if(checker.isConnect()):Here i have called the isConnected() and check the condition true or false if the condition is true then.

``

Toast.makeText( MainActivity.this,"Connected",Toast.LENGTH_LONG ).show() :It will print the message connected.

else:if the if condition is false then control come inside the else part and then.

Toast.makeText( MainActivity.this,"not Connected",Toast.LENGTH_LONG ).show():It will print the message not connected.


Internet Connection Checker

how to check the internet connection for video click here

back                     next