Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it OK to unregister 'dynamic' BroadcastReceiver from receiver's own onReceive() method?

That is, I have this BroadcastReceiver I create on the fly to listen for one broadast, after which I want it to unregister itself.

I haven't found any sample code that does it this way, but neither have I found any rule in the android online docs that forbids this. But I cannot let it hang around for as long as the activity, and it is in an anonymous class anyway, so the containing class does not even know the variable name.

That is, the code looks something like this:

myInfoReceiver = new BroadcastReceiver() {
onReceive(Context ctx, Intent intt) {
    // do some Notification when I get here
    nm.notify("I got here") // obvious pseudo code
    ctx.unregisterReceiver(myInfoReceiver);
} // end onReceive
ctx.registerReceiver),uInfoReceiver, new IntentFilter(...));
}; // end BroadcastReceiver

But when I run this, Android complains when it calls unregister, insisting that the receiver is not there to unregister (I forget the exact wording, but it threw IllegalArgumentException).

I also tried modifying the code to check that the action in 'intt' is the same as expected -- but then it still executest onReceive but silently fails to unregister.

like image 548
Matt J. Avatar asked Jun 11 '15 14:06

Matt J.


People also ask

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

Creating a BroadcastReceiver The onReceiver() method is first called on the registered Broadcast Receivers when any event occurs. The intent object is passed with all the additional data. A Context object is also available and is used to start an activity or service using context.

Which pair of methods do you use to register and unregister your broadcast receiver dynamically?

Receiver can be registered via the Android manifest file. You can also register and unregister a receiver at runtime via the Context. registerReceiver() and Context. unregisterReceiver() methods.

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 problem with declaring a broadcast receiver with implicit intents in AndroidManifest XML?

Essentially, the problem in Android Oreo with BroadcastReceivers is that all BroadcastReceivers that we have declared in our AndroidManifest. xml and are listening to implicit intents will no longer receive those broadcasts (there are some cases that are allowed).


1 Answers

The answer to your question is "yes". However...

...you need to call unregisterReceiver() on the same Context that you called registerReceiver() on. In the code you posted you are calling unregisterReceiver() on the Context passed in as an argument to onReceive(). This is not the same Context which is why you are getting the exception.

like image 73
David Wasser Avatar answered Sep 24 '22 05:09

David Wasser