4

How to unregister receiver attached to LocalBroadcastManager?

I tried

unregisterReceiver(broadcastReceiver);

but it gave me an exception

java.lang.IllegalArgumentException: Receiver not registered:

This is my code

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    init();
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiverCancelPd, new IntentFilter(PelicanApplication.identifierCancelPd));
    LocalBroadcastManager.getInstance(this).registerReceiver(bReceiverSignOut, new IntentFilter(PelicanApplication.identifierSignOut));
    LocalBroadcastManager.getInstance(this).registerReceiver(bReceiverNearByDeliveries, new IntentFilter(PelicanApplication.identifierNBD));
}


@Override
protected void onStop() {
    super.onStop();
    unregisterReceiver(broadcastReceiverCancelPd);
    unregisterReceiver(bReceiverSignOut);
    unregisterReceiver(bReceiverNearByDeliveries);
}
Shehan Ekanayake
  • 1,581
  • 4
  • 20
  • 35

5 Answers5

12

Calling unregisterReceiver() like you are is calling that method on the current Context, rather than the LocalBroadcastManager. You need to call LocalBroadcastManager.getInstance(this).unregisterReceiver();.

Mike M.
  • 38,532
  • 8
  • 99
  • 95
  • [LocalBroadcastManager has been deprecated](https://stackoverflow.com/q/61484995/6576302), do you know alternative of `unregisterReceiver()` using `LiveData`? or any? – C.F.G Jan 12 '23 at 15:19
  • I suppose [`removeObserver()`](https://developer.android.com/reference/androidx/lifecycle/LiveData#removeObserver(androidx.lifecycle.Observer%3C?%20super%20T%3E)) would be the analogous method, but you might not need to worry about it. Have a look at the description for [`observe()`](https://developer.android.com/reference/androidx/lifecycle/LiveData#observe(androidx.lifecycle.LifecycleOwner,androidx.lifecycle.Observer%3C?%20super%20T%3E)), too. – Mike M. Jan 12 '23 at 16:20
1

On stop do something like this

LocalBroadcastManager.getInstance(context).unregisterReceiver(this);
Srishti Roy
  • 576
  • 5
  • 17
0

Move your unregisterReceiver() functions to onDestroy().

siriscac
  • 1,699
  • 12
  • 21
0

For an Activity:

In order to register your broadcast receiver from within your app, first, remove the tag from your AndroidManifest.xml file. Then, call registerReceiver(BroadcastReceiver receiver, IntentFilter filter) in your onResume(). 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().

Naveen Kumar Mishra
  • 321
  • 1
  • 3
  • 16
0

I think your app crashes on this line:

unregisterReceiver(batteryNotifyReceiver);

If the receiver was not registered or already unregistered, then call to unregisterReceiver() throws IllegalArgumentException. So you have to check if receiver is registered or not before call unregistered.

if (batteryNotifyReceiver!= null) {
                    unregisterReceiver(batteryNotifyReceiver);
                    batteryNotifyReceiver= null;
            }

I hope your problem will be resolved. I solved this error in this way in my application. Give it a try and ping me back.

Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
Md Maidul Islam
  • 2,294
  • 3
  • 17
  • 22
  • Mike M. solution solved my problem(it was related to LocalBroadcastManager)...but i should follow your advice too.It's better to check if the receiver is not null and then unregister it. – Shehan Ekanayake Jun 01 '15 at 06:35