Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to unregister BroadcastReceiver? In onPause(), onDestroy(), or onStop()?

When should I use unregisterReceiver? In onPause(), onDestroy(), or onStop()?

Note: I need the service to run in the background.

Update:

  1. I get an exception releasing receivers null.

  2. Activity has leaked intent receivers are you missing call to unregisterReceiver();

Please tell me if there's something wrong, here's my code:

private boolean processedObstacleReceiverStarted; private boolean mainNotificationReceiverStarted;  protected void onResume() {      super.onResume();     try {         registerReceivers();      } catch (Exception e) {          Log.e(MatabbatManager.TAG,                 "MAINActivity: could not register receiver for Matanbbat Action "                         + e.getMessage());     } }  private void registerReceivers() {      if (!mainNotificationReceiverStarted) {         mainNotificationReceiver = new MainNotificationReceiver();          IntentFilter notificationIntent = new IntentFilter();          notificationIntent                 .addAction(MatabbatManager.MATABAT_LOCATION_ACTION);         notificationIntent                 .addAction(MatabbatManager.MATABAT_New_DATA_RECEIVED);         notificationIntent                 .addAction(MatabbatManager.STATUS_NOTIFCATION_ACTION);         registerReceiver(mainNotificationReceiver, notificationIntent);          mainNotificationReceiverStarted = true;      }      if (!processedObstacleReceiverStarted) {         processedObstacleReceiver = new ProcessedObstacleReceiver();         registerReceiver(processedObstacleReceiver, new IntentFilter(                 MatabbatManager.MATABAT_ALARM_LOCATION_ACTION));         processedObstacleReceiverStarted = true;      }  }  private void unRegisterReceivers() {      if (mainNotificationReceiverStarted) {         unregisterReceiver(mainNotificationReceiver);         mainNotificationReceiverStarted = false;     }     if (processedObstacleReceiverStarted) {         unregisterReceiver(processedObstacleReceiver);         processedObstacleReceiverStarted = false;     }  }   @Override protected void onDestroy() {     // TODO Auto-generated method stub     super.onDestroy();      try {          unRegisterReceivers();         mWakeLock.release();//keep screen on     } catch (Exception e) {         Log.e(MatabbatManager.TAG, getClass() + " Releasing receivers-" + e.getMessage());     }  } 
like image 224
Muhammad Avatar asked Jan 15 '14 11:01

Muhammad


People also ask

Do I need to unregister BroadcastReceiver?

It's always suggested to register and unregister broadcast receiver programmatically as it saves system resources.

Which of the following method is used to unregister a previously registered BroadcastReceiver?

Use unregisterReceiver(BroadcastReceiver receiver) in your onPause() to unregister the Broadcast receiver. For a Service: Remove the receiver tag from the manifest file. You can then register your Broadcast receiver with the same method in the onCreate() and unregister in the onDestroy() .

Where we should register and deregister the broadcast?

You should register and unregister your broadcast in onResume() and onPause() methods. if you register in onStart() and unregister it in onStop().

What is the role of the onReceive () method in the BroadcastReceiver?

Retrieve the current result extra data, as set by the previous receiver. This can be called by an application in onReceive(Context, Intent) to allow it to keep the broadcast active after returning from that function.


2 Answers

it depends on where you have register the receiver. The complementary method pairs are

onCreate - onDestroy onResume - onPause onStart  - onStop 

if you register the receiver in the first one then unregister it in it's ending method.

like image 51
stinepike Avatar answered Oct 05 '22 16:10

stinepike


From the Android documentation:

You should implement onStop() to release activity resources such as a network connection or to unregister broadcast receivers.

Then, I would follow these pairs (using @StinePike's analogy):

onResume - onPause onStart  - onStop 

Because of the Android Lifecycle, and as @w3bshark mentioned:

In post-HoneyComb (3.0+) devices, onStop() is the last guaranteed handler.

like image 38
Evin1_ Avatar answered Oct 05 '22 16:10

Evin1_



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!