Results 1 to 2 of 2
  1. #1

    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:

    planInfoReceiver = new BroadcastReceiver() {
    onReceive(Context ctx, Intent intt) {
    // do some Notification when I get here
    nm.notify("I got here") // obvious pseudo code
    ctx.unregisterReceiver(planInfoReceiver);
    } // end onReceive
    ctx.registerReceiver(planInfoReceiver, 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 included an IllegalArgumentException).

    I also tried modifying the code to check that the action in 'intt' is the same as expected -- but then it still executets onReceive but silently fails to unregister.
    Last edited by mejohnsn; 06-11-2015 at 10:24 AM.

  2. #2
    Hi,

    I'm facing the same problem and I haven't found information about how to unregister all receiverbroadcast of one app or unregister one especific receiver from other activity or class which weren't made the register.

    ┐Have you found any answer about it?

    Regards
    Rodrigo



Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •