Check Internet Connectivity in Android

Introduction

 
This article explains how to check an internet connection in Android. Android Studio is used to create the sample. For this, first in your XML file you will use a button and on its click, you will check an internet connection.
 
In your Java file, you will create the id of a button and set it on its click event. Inside the button click you will determine whether the internet connection is present that will be returned by the ConnectingInternet() method. If it returns true then an alert dialogue is shown that contains the text "Internet-connected" otherwise the internet is not connected.
 
In the "ConnectingInternet()" method you will get the object of the Connectivity manager class by calling "getSystemSerivces()". After getting the object you will determine whether the connectivity is not null then get all the information about the network by calling the "getAllnetworkInfo()" method. Determine whether the about info is not equal to null and if it is not equal to null then it returns true otherwise false.
 
Step 1
 
Create a project like this:
 
checkinternet3
 
Step 2
 
Create an XML file and write the following.
 
In your XML file, you will use a button and on its click, you will check for an internet connection.
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  6.     android:paddingRight="@dimen/activity_horizontal_margin"  
  7.     android:paddingTop="@dimen/activity_vertical_margin"  
  8.     android:paddingBottom="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity"  
  10.     android:background="#000000">  
  11.    
  12.     <TextView  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="checkInternet"  
  16.         android:textStyle="bold"  
  17.         android:textSize="30dp"  
  18.         android:layout_centerHorizontal="true"/>  
  19.    
  20.     <Button  
  21.         android:id="@+id/check_button"  
  22.         android:layout_width="wrap_content"  
  23.         android:layout_height="wrap_content"  
  24.         android:layout_centerInParent="true"  
  25.         android:text="checkInternet" />  
  26.    
  27. </RelativeLayout> 
Step 3
 
Create a Java file with the following code.
 
In your Java file, you will create the id of a button and set it on its click event. Inside the button click you will determine whether an internet connection is present that will be returned by the ConnectingInternet() method. If it returns true then an alert dialogue is shown that contains the text "Internet-connected" otherwise the internet is not connected.
 
In the "ConnectingInternet()" method you will get the object of the Connectivity Manager class by calling "getSystemSerivces()". After getting the object you will determine whether the connectivity is not null then get all the information about the network by calling the "getAllnetworkInfo()" method. Check the about info to determine whether it is not equal to null; if it is not equal to null then it returns true otherwise false.
  1. package com.projectinternet;    
  2.     
  3. import android.app.Activity;    
  4. import android.app.AlertDialog;    
  5. import android.content.Context;    
  6. import android.content.DialogInterface;    
  7. import android.os.Bundle;    
  8. import android.view.View;    
  9. import android.widget.Button;    
  10.     
  11. public class MainActivity extends Activity     
  12. {    
  13.  Boolean InternetAvailable = false;    
  14.  Button checkInternet;    
  15.  Seocnd detectconnection;    
  16.  @Override    
  17.  public void onCreate(Bundle savedInstanceState)     
  18.  {    
  19.   super.onCreate(savedInstanceState);    
  20.   setContentView(R.layout.activity_main);    
  21.   checkInternet = (Button) findViewById(R.id.check_button);    
  22.   detectconnection = new Seocnd(getApplicationContext());    
  23.   checkInternet.setOnClickListener(new View.OnClickListener()     
  24.   {    
  25.    @Override    
  26.    public void onClick(View v)     
  27.    {    
  28.     InternetAvailable = detectconnection.InternetConnecting();    
  29.     if (InternetAvailable)     
  30.     {    
  31.      showAlertDialog(MainActivity.this"Internet Connection",    
  32.       "internet is available"true);    
  33.     }     
  34.     else     
  35.     {    
  36.      showAlertDialog(MainActivity.this"No Internet Connection",    
  37.       "internet is not available"false);    
  38.     }    
  39.    }    
  40.   });    
  41.  }    
  42.  public void showAlertDialog(Context context, String t, String m, Boolean status)     
  43.  {    
  44.   AlertDialog message = new AlertDialog.Builder(context).create();    
  45.   message.setTitle(t);    
  46.   message.setMessage(m);    
  47.   // message.setIcon((status) ? R.drawable.success : R.drawable.fail);    
  48.   message.setButton("OK"new DialogInterface.OnClickListener()     
  49.   {    
  50.    public void onClick(DialogInterface dialog, int which)     
  51.    {    
  52.    }    
  53.   });    
  54.   message.show();    
  55.  }    
  56. }   
Step 4
 
Create another Java file and write this:
  1. package com.projectinternet;  
  2. import android.content.Context;  
  3. import android.net.ConnectivityManager;  
  4. import android.net.NetworkInfo;  
  5.    
  6. public class Seocnd {  
  7.     private Context context;  
  8.     public Seocnd(Context c){  
  9.         this.context = c;  
  10.     }  
  11.     public boolean InternetConnecting(){  
  12.         ConnectivityManager connect = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);  
  13.         if (connect != null)  
  14.         {  
  15.             NetworkInfo[] information = connect.getAllNetworkInfo();  
  16.             if (information != null)  
  17.                 for (int x = 0; x < information.length; x++)  
  18.                     if (information[x].getState() == NetworkInfo.State.CONNECTED)  
  19.                     {  
  20.                         return true;  
  21.                     }  
  22.         }  
  23.         return false;  
  24.     }  
Step 5
 
Add internet permission in the Android "Menifest.xml" file as in the following:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.projectinternet"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.    
  7.     <uses-sdk  
  8.         android:minSdkVersion="7"  
  9.         android:targetSdkVersion="16" />  
  10.    
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.projectinternet.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.    
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.     </application>  
  26.     <uses-permission android:name="android.permission.INTERNET" />  
  27.     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
  28.    
  29. </manifest> 
Step 6
 
Check the internet connection as in the following:
 
checkinternet
 
checkinternet2


Similar Articles