Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unregistering Android Broadcast Receiver in onReceive throws "Receiver not registered"

I have a BroadcastReceiver for one time use.

I'm registering it in an Activity. I can't put the unregisterReceiver() in onPause because it has to stay running even when the activity is paused or destroyed.

I want the BroadcastReceiver to unregister itself when it is done, something like this:

public class SmsReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
           // do some code..
           context.unregisterReceiver(this)
        }
}

But it causes an Exception: Receiver not registered.

like image 306
shaimagz Avatar asked Feb 10 '11 13:02

shaimagz


People also ask

How do I know if my broadcast receiver is registered?

registerReceiver(BroadcastReceiver,IntentFilter) */ public Intent register(Context context, IntentFilter filter) { try { // ceph3us note: // here I propose to create // a isRegistered(Contex) method // as you can register receiver on different context // so you need to match against the same one :) // example by ...

What is onReceive?

onReceive(Context context, Intent intent) This method is called when the BroadcastReceiver is receiving an Intent broadcast. IBinder. peekService(Context myContext, Intent service) Provide a binder to an already-bound service.

What does the receiver tag in Android manifest refer to?

Broadcast receivers enable applications to receive intents that are broadcast by the system or by other applications, even when other components of the application are not running. There are two ways to make a broadcast receiver known to the system: One is declare it in the manifest file with this element.


3 Answers

A BroadcastReceiver only exists during the execution of its onReceive() method. So, this evaluates to a throw-away instance every time the broadcast is fired/received. See Broadcast Receiver Lifecycle. For dynamically registering/unregistering of BroadcastReceivers, you have to remember the instance of your receiver in onPause() to register it again during onResume().

like image 66
tichy Avatar answered Oct 18 '22 01:10

tichy


I know this question already has an answer But try this Code

This code is for BatteryInfo. And it did work.

private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context arg0, Intent intent) {
        // TODO Auto-generated method stub
        int level = intent.getIntExtra("level", 0);
        Log.i("Battery", String.valueOf(level) + "%");
        arg0.unregisterReceiver(mBatInfoReceiver);
    }

};

//Below code is the Code which attaches the reciever put this code in which ever place you want. 

this.registerReceiver(this.mBatInfoReceiver, new IntentFilter(
                Intent.ACTION_BATTERY_CHANGED));

Reference for Attaching StackOverFlow / Tutorial

like image 24
MDMalik Avatar answered Oct 18 '22 00:10

MDMalik


Just add a method call to the Activity you registered it in, and in that method, unregister the receiver. That way you know its job has been done.

However, the receiver won't work if the activity is stopped or destroyed anyways. You'd have to declare it in the manifest or else register it in a long-running service to keep it working outside of an activity's lifetime. As far as I know, you can't unregister a receiver declared in the manifest because you don't get the instance it's registered with. But the method call to the Service to unregister it should work.

If you just need a worker for one-time use, declare an exported IntentService in the manifest with the action you're broadcasting. That IntentService will be started when the intent is received and given the intent that started it, and then when it's done its job, it will stop on its own.

like image 4
Austin B Avatar answered Oct 18 '22 01:10

Austin B