title: Detect if the application is manually closed or not
body: <p>I've written an android app which checks the network status by using a BroadcastReceiver inherited class:</p>
  
  <pre><code>public class NetworkChangeReceiver extends BroadcastReceiver {
      @Override
      public void onReceive(final Context context, final Intent intent) {
          Log.d("mylog", "NetworkChangeReceiver Hit");
      }
  }
  </code></pre>
  
  <p>which is registered in the manifest file like this:</p>
  
  <pre><code>&lt;receiver
      android:name="foo.NetworkChangeReceiver"
      android:label="NetworkChangeReceiver" &gt;
      &lt;intent-filter&gt;
          &lt;action android:name="android.net.conn.CONNECTIVITY_CHANGE" /&gt;
          &lt;action android:name="android.net.wifi.WIFI_STATE_CHANGED" /&gt;
      &lt;/intent-filter&gt;
  &lt;/receiver&gt;
  </code></pre>
  
  <p>I needed to handle the internet connection whenever it connects or disconnects. Actually, it works perfectly in normal situations.</p>
  
  <p>But the problem is that when the application is closed manually (by minimizing the app and then closing it by swiping out the app icon in the <em>Recents</em> button menu), it still receives the network status changes. This sometimes causes some exceptions. </p>
  
  <p>Even I have included all the code in receiver function in try/catch block, but still sometimes a toast message containing an error message is shown. This sometimes happen even after some days after the closure of the app.</p>
  
  <p>Please note that the code in the receiver function is more complicated than the code that is shown here and has some access to internal classes and variables.</p>