How to unregister a receiver in activity?
<p>Iam working with chat application.</p>

<p>In onPause() and in onResume() I registered the receiver.</p>

<p>I cannot unregister receiver in onPause() because in receiver Iam checking for the network connection</p>

<p>In onPause and onResume I have to monitor the network</p>

<p>Then Iam in confusion that where to unregister receiver?</p>

<p>Could anyone please help me.</p>

<pre><code>protected void onPause(){
        backfacekey=1;


        //unregisterReceiver(mConnReceiver); 
        super.onPause();
        registerReceiver(mFacebookReceiver, 
                  new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
    }


    protected void onResume(){

        backfacekey=0;
        super.onResume();

        registerReceiver(mFacebookReceiver, 
                 new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

    }



    private BroadcastReceiver mFacebookReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {



                 NetworkInfo info = (NetworkInfo)intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO); 


                 if (info.getState().equals(NetworkInfo.State.DISCONNECTED)) {
                  Toast.makeText(getApplicationContext(), "Network disconnected",Toast.LENGTH_LONG).show();

                 }
                  key=2;

                 }

                   if (info.getState().equals(NetworkInfo.State.CONNECTED)) {

                     if(key==2){



                                Toast.makeText(getApplicationContext(), "Network connected ",Toast.LENGTH_LONG).show(); 
                key=0;

                        }
                     }
                    };
</code></pre>
