Solve error has leaked IntentReceiver on Android

4

I have an extended class of BroadcastReceiver in MainActivity.java I launch the listener

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

I find that when I leave the app it shows me the following error in the log:

  

E / ActivityThread: Activity   com.webserveis.app.myconnection.MainActivity has leaked IntentReceiver   [email protected]32e   that was originally registered here. Are you missing a call to   unregisterReceiver ()?                         android.app.IntentReceiverLeaked: Activity com.webserveis.app.myconnection.MainActivity has leaked IntentReceiver   [email protected]32e   that was originally registered here. Are you missing a call to   unregisterReceiver ()?

     

that was originally registered here. Are you missing a call to   unregisterReceiver ()

In onStop() I would like to deregister the listener, but not as implement it

    
asked by Webserveis 29.06.2016 в 11:38
source

2 answers

2

You should save your BroadcastReceiver in a variable of your MainActivity and in your onStop() put unregisterReceiver(myBroadcastReceiver);

You should keep in mind that if you go through the onStop() without having registered the BroadcastReceiver previously, an exception will be thrown.

Greetings

    
answered by 29.06.2016 / 12:01
source
4

When implementing a BroadcastReceiver , it is important to know that we need to register and deregister the receiver , but It is NOT good practice to cancel the receiver on onStop () and this is the reason, which you can see in the documentation:

When your implementation requires registration in onResume() there is a consideration important .

  

If you are registering a receiver in Activity.onResume() , you must cancel   the record in Activity.onPause() . (You will not receive intents when you are in   pause, and this can reduce unnecessary system overhead)

Regarding your error:

  

MainActivity has leaked IntentReceiver

the documentation itself points to it:

  

Do not forget to cancel the registered receiver register in   dynamic using the Context.unregisterReceiver() method. If you forget   of this, the Android system reports a Leaked error. For example, yes   You registered a receiver in onResume() of your activity, you must    cancel registration in the onPause() method.

Therefore:

  

In the onResume () method, the Recording receiver.

     

In the onPause () method, the deregistration of the receiver.

I add an example:

public class MyActivity extends Activity
{

  private final BroadcastReceiver mybroadcast = new BroadcastReceiver ();

  //REGISTRA!
  public void onResume()
  {
    IntentFilter filter = new IntentFilter();
    filter.addAction("android.provider.Telephony.SMS_RECEIVED");
    registerReceiver(mybroadcast, filter);  

  }

  //CANCELA!
  public void onPause()
  {
    unregisterReceiver(mybroadcast);
  }
}
    
answered by 29.06.2016 в 17:01