title: Check network connectivity android
body: <p>I am new using <code>Service</code> in Android and I am quite confused. I have and <code>Activity</code> and I want to check for all the time that the network connection is up (wifi or 3g). I implemented a simple <code>Service</code> and a <code>BroadcastReveiver</code>. </p>
  
  <p>here the code of the <code>Service</code></p>
  
  <pre><code>public class NetworkCheck extends IntentService {
  
  private final String CONNECTION = "connection";
  
  public NetworkCheck(String name) {
      super(name);
  }
  
  @Override
  protected void onHandleIntent(Intent workIntent) {
  
      ConnectivityManager conMgr = (ConnectivityManager)getSystemService(getApplicationContext().CONNECTIVITY_SERVICE);
  
  
      String dataString = workIntent.getDataString();
  
      if ( conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED
              || conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED) {
  
          // notify user you are not online
          Intent localIntent = new Intent().putExtra(CONNECTION, 0);
  
          LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);
  
      }
  }
  </code></pre>
  
  <p>here the code of the <code>BroadcastReceiver</code></p>
  
  <pre><code>public class NetworkReceiver extends BroadcastReceiver{
  
      @Override
      public void onReceive(Context context, Intent intent) {
  
       int value = intent.getIntExtra("connection", 0);
       if(value == 0){
           //now I print, then I will show up a dialog
           System.out.println("NO CONNECTION!!!!!!!!");
  
       }
  
      }
  }
  </code></pre>
  
  <p>this is the user permission in the <code>Manifest</code></p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
  </code></pre>
  
  <p>and here there is the code that I put in the <code>onCreate</code> method of my <code>Activity</code></p>
  
  <pre><code>this.intent = new Intent(this, NetworkCheck.class);
          this.startService(this.intent);
  
          LocalBroadcastManager.getInstance(this).registerReceiver(new NetworkReceiver(), new IntentFilter());
  </code></pre>
  
  <p>This does not work and I cannot figure out why.</p>