Check the status of Internet Connection while Opening an Application in Android

For this purpose we will use ConnectivityManager and by using getAllNetworkInfo we can get Network Information and can get state of Network.

With the help of NetworkInfo.State.CONNECTED.

We need to add permissions of ACCESS_NETWORK_STATE and INTERNET in AndroidManifest file .
For instance:

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

ConnectivityManager:

  1. It is used to monitor network connections (Wi-Fi, GPRS, UMTS, etc.).
  2. It is also used to send broadcast intents when network connectivity changes.

getAllNetworkInfo ():

It returns connection status information about all network types supported by the device.

Method:

  1. public static boolean internetConnectionCheck(Activity CurrentActivity) {  
  2.     Boolean Connected = false;  
  3.     ConnectivityManager connectivity = (ConnectivityManager) CurrentActivity.getApplicationContext()  
  4.         .getSystemService(Context.CONNECTIVITY_SERVICE);  
  5.     if (connectivity != null) {  
  6.         NetworkInfo[] info = connectivity.getAllNetworkInfo();  
  7.         if (info != nullfor (int i = 0; i < info.length; i++)  
  8.         if (info[i].getState() == NetworkInfo.State.CONNECTED) {  
  9.             Log.e("My Network is: ""Connected");  
  10.             Connected = true;  
  11.         } else {}  
  12.     } else {  
  13.         Log.e("My Network is: ""Not Connected");  
  14.   
  15.         Toast.makeText(CurrentActivity.getApplicationContext(),  
  16.             "Please Check Your internet connection",  
  17.         Toast.LENGTH_LONG).show();  
  18.         Connected = false;  
  19.   
  20.     }  
  21.     return Connected;  
  22. }  
Source Code:

MainActivity.java
  1. package com.example.internetconnectioncheckbyabhi;  
  2.   
  3. import android.support.v7.app.ActionBarActivity;  
  4. import android.support.v7.app.ActionBar;  
  5. import android.support.v4.app.Fragment;  
  6. import android.app.Activity;  
  7. import android.content.Context;  
  8. import android.net.ConnectivityManager;  
  9. import android.net.NetworkInfo;  
  10. import android.os.Bundle;  
  11. import android.util.Log;  
  12. import android.view.LayoutInflater;  
  13. import android.view.Menu;  
  14. import android.view.MenuItem;  
  15. import android.view.View;  
  16. import android.view.ViewGroup;  
  17. import android.widget.TextView;  
  18. import android.widget.Toast;  
  19. import android.os.Build;  
  20.   
  21. public class MainActivity extends ActionBarActivity {  
  22.   
  23.     @Override  
  24.     protected void onCreate(Bundle savedInstanceState) {  
  25.         super.onCreate(savedInstanceState);  
  26.         setContentView(R.layout.activity_main);  
  27.   
  28.   
  29.         if (savedInstanceState == null) {  
  30.             getSupportFragmentManager().beginTransaction()  
  31.                 .add(R.id.container, new PlaceholderFragment()).commit();  
  32.         }  
  33.   
  34.         if (internetConnectionCheck(MainActivity.this)) {  
  35.             Toast.makeText(getApplicationContext(), "Internet Connection is available. Abhijeet !!!"4000).show();  
  36.   
  37.         } else  
  38.   
  39.         {  
  40.             Toast.makeText(getApplicationContext(), "No Internet Connection Try Later !", Toast.LENGTH_SHORT).show();  
  41.         }  
  42.     }  
  43.   
  44.     @Override  
  45.     public boolean onCreateOptionsMenu(Menu menu) {  
  46.   
  47.   
  48.         getMenuInflater().inflate(R.menu.main, menu);  
  49.         return true;  
  50.     }  
  51.   
  52.     @Override  
  53.     public boolean onOptionsItemSelected(MenuItem item) {  
  54.   
  55.         int id = item.getItemId();  
  56.         if (id == R.id.action_settings) {  
  57.             return true;  
  58.         }  
  59.         return super.onOptionsItemSelected(item);  
  60.     }  
  61.   
  62.     public static class PlaceholderFragment extends Fragment {  
  63.   
  64.         public PlaceholderFragment() {}  
  65.   
  66.         @Override  
  67.         public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  68.         Bundle savedInstanceState) {  
  69.             View rootView = inflater.inflate(R.layout.fragment_main, container,  
  70.             false);  
  71.   
  72.             return rootView;  
  73.         }  
  74.     }  
  75.   
  76.     public static boolean internetConnectionCheck(Activity CurrentActivity) {  
  77.         Boolean Connected = false;  
  78.         ConnectivityManager connectivity = (ConnectivityManager) CurrentActivity.getApplicationContext()  
  79.             .getSystemService(Context.CONNECTIVITY_SERVICE);  
  80.         if (connectivity != null) {  
  81.             NetworkInfo[] info = connectivity.getAllNetworkInfo();  
  82.             if (info != nullfor (int i = 0; i < info.length; i++)  
  83.             if (info[i].getState() == NetworkInfo.State.CONNECTED) {  
  84.                 Log.e("My Network is: ""Connected ");  
  85.                 Connected = true;  
  86.             } else {}  
  87.   
  88.         } else {  
  89.             Log.e("My Network is: ""Not Connected");  
  90.   
  91.             Toast.makeText(CurrentActivity.getApplicationContext(),  
  92.                 "Please Check Your internet connection",  
  93.             Toast.LENGTH_LONG).show();  
  94.             Connected = false;  
  95.   
  96.         }  
  97.         return Connected;  
  98.   
  99.     }  
  100.   
  101. }  
activity_main.xml
  1. <FrameLayout  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:id="@+id/container"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="com.example.internetconnectioncheckbyabhi.MainActivity"  
  8.     tools:ignore="MergeRootFrame" />  
fragment_main.xml
  1. <RelativeLayout  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:paddingBottom="@dimen/activity_vertical_margin"  
  7.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  8.     android:paddingRight="@dimen/activity_horizontal_margin"  
  9.     android:paddingTop="@dimen/activity_vertical_margin"  
  10.     tools:context="com.example.internetconnectioncheckbyabhi.MainActivity$PlaceholderFragment" >  
  11.     <TextView  
  12.     android:id="@+id/textView1"  
  13.     android:layout_width="wrap_content"  
  14.     android:layout_height="wrap_content"  
  15.     android:text="@string/hello_world" />  
  16. </RelativeLayout>  
Output:

Output