title: Why BroadcastReceiver works even when app is in background ?
body: <p>I am checking Internet connectivity in my app using BroadcastReceiver and I show an alert dialog if the connection is lost. It works fine. But my problem is that BroadcastReceiver works even if my app is in backgroung. So dialog pops up when internet connection is lost even if user is in some other app. This is totally ruining my app. </p>
  
  <p>Has anyone got any idea how to restrict Broadcast receiver in the app only? </p>
  
  <p>Here is my BroadcastReceiver : </p>
  
  <pre><code> public class ConnectivityChangedReceiver extends BroadcastReceiver{
  
      @Override
      public void onReceive( Context context, Intent intent )
       {
          ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE );
      NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
      if (networkInfo != null &amp;&amp; networkInfo.isConnected()) {
      } else {
          try{
              Intent i=new Intent(context, InternetDialogActivity.class);
              i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              context.startActivity(i);
          } catch(Exception e){
              e.printStackTrace();
          }
      }
    }
  }
  </code></pre>
  
  <p>And the activity is:</p>
  
  <pre><code> public class InternetDialogActivity extends Activity implements OnClickListener{
     @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.internet_dialog_box);
        getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        Button retryButton = (Button) findViewById(R.id.retryInternetButton);
        retryButton.setOnClickListener(this);
      }
  
     public boolean checkConnectivity(){
         ConnectivityManager connectivityManager   = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
             NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
  
         if (networkInfo!=null &amp;&amp; networkInfo.isConnected()) {
             finish();
             return true;
         } else {
             Intent intent = getIntent();
             finish();
             startActivity(intent);
             return false; 
        }
     }
  
     @Override
     public void onClick(View view) {
        switch(view.getId()){
           case R.id.retryInternetButton:
               try{
                   checkConnectivity();
               } catch(Exception e){
                   e.printStackTrace();
               }
               break;
         }
     }
     }
  </code></pre>
  
  <p>Here is how I declared receiver and activity in manifest:</p>
  
  <pre><code>  &lt;receiver android:name="com.lisnx.service.ConnectivityChangedReceiver"
          android:label="NetworkConnection"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.net.conn.CONNECTIVITY_CHANGE"/&gt;
          &lt;/intent-filter&gt;
    &lt;/receiver&gt;
  
    &lt;activity android:name="com.lisnx.activity.InternetDialogActivity"
          android:configChanges="orientation|keyboardHidden"
          android:theme="@android:style/Theme.Dialog" /&gt;
  </code></pre>
  
  <p>I have read that we can restrict BroadcastReceiver to work within the app by not declaring it in the manifest. But I don't know how will receiver work then? Please help me. I am stuck on it badly. Thanx in advance.</p>