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:
- It is used to monitor network connections (Wi-Fi, GPRS, UMTS, etc.).
- 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:
- public static boolean internetConnectionCheck(Activity CurrentActivity) {
- Boolean Connected = false;
- ConnectivityManager connectivity = (ConnectivityManager) CurrentActivity.getApplicationContext()
- .getSystemService(Context.CONNECTIVITY_SERVICE);
- if (connectivity != null) {
- NetworkInfo[] info = connectivity.getAllNetworkInfo();
- if (info != null) for (int i = 0; i < info.length; i++)
- if (info[i].getState() == NetworkInfo.State.CONNECTED) {
- Log.e("My Network is: ", "Connected");
- Connected = true;
- } else {}
- } else {
- Log.e("My Network is: ", "Not Connected");
-
- Toast.makeText(CurrentActivity.getApplicationContext(),
- "Please Check Your internet connection",
- Toast.LENGTH_LONG).show();
- Connected = false;
-
- }
- return Connected;
- }
Source Code:
MainActivity.java
- package com.example.internetconnectioncheckbyabhi;
-
- import android.support.v7.app.ActionBarActivity;
- import android.support.v7.app.ActionBar;
- import android.support.v4.app.Fragment;
- import android.app.Activity;
- import android.content.Context;
- import android.net.ConnectivityManager;
- import android.net.NetworkInfo;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.TextView;
- import android.widget.Toast;
- import android.os.Build;
-
- public class MainActivity extends ActionBarActivity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
-
- if (savedInstanceState == null) {
- getSupportFragmentManager().beginTransaction()
- .add(R.id.container, new PlaceholderFragment()).commit();
- }
-
- if (internetConnectionCheck(MainActivity.this)) {
- Toast.makeText(getApplicationContext(), "Internet Connection is available. Abhijeet !!!", 4000).show();
-
- } else
-
- {
- Toast.makeText(getApplicationContext(), "No Internet Connection Try Later !", Toast.LENGTH_SHORT).show();
- }
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
-
-
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
-
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
-
- int id = item.getItemId();
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
-
- public static class PlaceholderFragment extends Fragment {
-
- public PlaceholderFragment() {}
-
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View rootView = inflater.inflate(R.layout.fragment_main, container,
- false);
-
- return rootView;
- }
- }
-
- public static boolean internetConnectionCheck(Activity CurrentActivity) {
- Boolean Connected = false;
- ConnectivityManager connectivity = (ConnectivityManager) CurrentActivity.getApplicationContext()
- .getSystemService(Context.CONNECTIVITY_SERVICE);
- if (connectivity != null) {
- NetworkInfo[] info = connectivity.getAllNetworkInfo();
- if (info != null) for (int i = 0; i < info.length; i++)
- if (info[i].getState() == NetworkInfo.State.CONNECTED) {
- Log.e("My Network is: ", "Connected ");
- Connected = true;
- } else {}
-
- } else {
- Log.e("My Network is: ", "Not Connected");
-
- Toast.makeText(CurrentActivity.getApplicationContext(),
- "Please Check Your internet connection",
- Toast.LENGTH_LONG).show();
- Connected = false;
-
- }
- return Connected;
-
- }
-
- }
activity_main.xml
- <FrameLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/container"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="com.example.internetconnectioncheckbyabhi.MainActivity"
- tools:ignore="MergeRootFrame" />
fragment_main.xml
- <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.example.internetconnectioncheckbyabhi.MainActivity$PlaceholderFragment" >
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/hello_world" />
- </RelativeLayout>
Output: