Android automatically detect internet connection status

If you have an android application that needs to sync data with a backend server but doesn't need the internet to be available all the time for it to work then you must know at all times whether your app has access to internet or not. As soon as you have internet connection you can get latest data from the server or post any changes saved in your app using HTTP service. In this example we extend the BroadcastReceiver to listen for any changes in network status and then find out whether the app has internet at that time. Please note that you have to specify the ACCESS_NETWORK_STATE in your android manifest permissions for this to work otherwise you will get java.lang.SecurityException: ConnectivityService: Neither user XXXXX nor current process has android.permission.ACCESS_NETWORK_STATE.

Android automatically detect internet connection status Android Internet connection status

Android Application Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    android:versionName="1.0" >

        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        android:theme="@style/AppTheme" >
            android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />


Android Application Layout

<RelativeLayout xmlns:android=""
    tools:context=".MainActivity" >

        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

Android Application Main Activity

package com.as400samplecode;

import android.os.Bundle;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

 private static final String LOG_TAG = "CheckNetworkStatus";
 private NetworkChangeReceiver receiver;
 private boolean isConnected = false;
 private TextView networkStatus;

 protected void onCreate(Bundle savedInstanceState) {

  IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
  receiver = new NetworkChangeReceiver();
  registerReceiver(receiver, filter);
  networkStatus = (TextView) findViewById(;

 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(, menu);
  return true;
 protected void onDestroy() {
  Log.v(LOG_TAG, "onDestory");


 public class NetworkChangeReceiver extends BroadcastReceiver {

  public void onReceive(final Context context, final Intent intent) {

   Log.v(LOG_TAG, "Receieved notification about network status");


  private boolean isNetworkAvailable(Context context) {
   ConnectivityManager connectivity = (ConnectivityManager) 
   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.v(LOG_TAG, "Now you are connected to Internet!");
        networkStatus.setText("Now you are connected to Internet!");
        isConnected = true;
        //do your processing here ---
        //if you need to post any data to the server or get status
        //update from the server
       return true;
   Log.v(LOG_TAG, "You are not connected to Internet!");
   networkStatus.setText("You are not connected to Internet!");
   isConnected = false;
   return false;



  1. thanks for post. but there is a one point i don't understand.

    if(info[i].getState()==NetworkInfo.State.CONNECTED) {

    Why !isConnected.

    Best Regards

    1. Yeah I know that's confusing but the NetworkChangeReceiver was getting called multiple times. May be its fixed in the current version. So all I do is if we already have an internet then ignore the next request, basically if you want to kickoff a process as soon as get internet then you don't want that action done more than once.


    2. The share your really gives us excitement. Thanks for your sharing. If you feel tired at work or study try to participate in our games to bring the most exciting feeling. Thank you!
      hotmail sign in | red ball game |

  2. Insipid now looking at life fifa 14 coins, I always hope that when the President so as romantic love, then warm. Every time I go to his business trip to the capital of poetry, he made a number of text cheap fifa 14 coins messages, hoping to use text messages as in the past to convey love and always have to ignore the rationale for each gas in their own death. Since some of his daughter, he and cold weather to give her daughter fifa 14 coins more than the clothes to wear; daughter learning to walk, and she let fall a good;

  3. Awsom Tutorial Thank You so much

  4. Great informative and useful article. thanks for sharing this information with us. Business Blog Updates

  5. I didn't get output!!!!!! plz help me...

  6. All the best blogs that is very useful for keeping me share the ideas
    of the future as well this is really what I was looking for, and I am
    very happy to come here. Thank you very much
    earn to die
    earn to die 2
    earn to die 3
    Hi! I’ve been reading your blog for a while now and finally got the
    earn to die 4
    courage to go ahead and give youu a shout out from
    earn to die 6
    Austin Texas! Just wanted to tell
    earn to die 5
    Hi! I’ve been reading your blog for a while now and finally got the
    happy wheels
    strike force heroes
    you keep up the fantastic work!my weblog
    age of war
    earn to die 5
    good game empire

  7. This piece of code is not working when device is connected with wifi but not having internet access.

    1. This comment has been removed by the author.

    2. i am looking for same but all these tutorials are not eplaingg that. Someone shhould help me. please if u get a code for same let me know or email me najeebputhiyallam AT

  8. This comment has been removed by the author.


  9. In manifest file we need to add the below kinda code snippet



  10. Very efficiently written information. It will be valuable to everyone who uses it, including myself. Thanks a lot!!

  11. Very interesting blog. Alot of blogs I see these days don't really provide anything that I'm interested in, but I'm most definately interested in this one. Just thought that I would post and let you know.
    excel | gta 5

  12. This is one of the cult game now, a lot of people enjoy playing them . Also you can refer to the game :
    gold mine strike | pokemon go 2
    The game controls are shown just under . Movement mechanisms primarily include acceleration and tilting controls.
    stickman games | stick war 2 | animal jam 2

  13. we like to honor numerous other world wide web web pages around the web, even though they arent linked to us, by linking to them. Below are some webpages worth checking out
    Jasa Seo Murah Terbaik Terpercaya anatara Jasa Seo


  14. I was noticing from many days that your blog is helping too many readers as your articles are easy to understand and helpful to us.
    I think you should explore the articles on your website, You should also cover different different categories for articles as you writes awesome. Thanks for sharing this great article with us.

    happy birthday wishes
    happy birthday
    good night
    good morning

  15. The blog or and best that is extremely useful to keep I can share the ideas of the future as this is really what I was looking for, I am very comfortable and pleased to come here. Thank you very much.
    animal jam | five nights at freddy's | hotmail login

  16. Your article is very good and useful, thank you very much for this content.
    windows movie maker

  17. How can I use this code for multiple activity to check internet connection? Can anyone help me on this?

  18. Don't wait to check it out - teensafe reviews. Tracking is accessible now!