Receiver Not Registered Exception Error

Receiver not registered exception error?

The root of your problem is located here:

 unregisterReceiver(batteryNotifyReceiver);

If the receiver was already unregistered (probably in the code that you didn't include in this post) or was not registered, then call to unregisterReceiver throws IllegalArgumentException. In your case you need to just put special try/catch for this exception and ignore it (assuming you can't or don't want to control number of times you call unregisterReceiver on the same recevier).

java.lang.IllegalArgumentException: Receiver not registered

If you register in onCreate(), you have to unregister in onDestroy(). If you want to unregister in onStop() you have to register in onStart().

Have a look at the activity lifecycle here http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

The reason for this is that onStop() is called when the Activity goes into the background, but is not necessarily destroyed. When the Activity comes back to the foreground onStart() is called, but not onCreate() so the BroadcastReceiver isn't re-registered. Then when the Activity goes back into the background, onStop() tries to unregister again, but the receiver has not been registered.

You also need to use the LocalBroadcastManager to unregister your receiver if you used it to register it like so:

LocalBroadcastManager.getInstance(this).unregisterReceiver(bReceiver);

LocalBroadcastManager is a class from the support library:

Helper to register for and send broadcasts of Intents to local objects within your process.

This is different from the same methods on Context which allow system-wide broadcasts.

Also see a similar question/answer here.

Weird Receiver not registered exception

We've seen this error when doing a long press on a particular screen, and then immediately doing two orientation changes (e.g. turning the device upside down).

The API docs for unregisterReceiver says:

Unregister a previously registered BroadcastReceiver.

It doesn't say explicitly, but as you've seen, you hit IllegalArgumentException: Receiver not registered if it isn't already registered.

The workaround I'm using is to store my Receiver as a member field, and have it set to null whenever it is not registered, i.e. I initialize it to null, and then only set it when I register it. This might not be perfect, but it does solve my crashes!

private Receiver mReceiver = null;

From my onServiceConnected:

sLog.debug("Registering receiver");
mReceiver = new Receiver();
registerReceiver(mReceiver, filter);

From my onServiceDisconnected:

if (mReceiver == null)
{
sLog.info("Do not unregister receiver as it was never registered");
}
else
{
sLog.debug("Unregister receiver");
unregisterReceiver(mReceiver);
mReceiver = null;
}

Receiver not registered error

Your BroadcastReceiver mReceiver is not initialized before registerReceiver in the onCreate, mReceiver is null when you register it. Although you create it later in getDiscoveredDevices but it did not registered there. As for that, you try to unregister a receiver which is actually never been registered.
So first create your receiver then register it not vica versa.

IllegalArgumentException: Receiver not registered

It seems you have some problems in your view. If you look into your stacktrace, there is some view which is causing problem and hence your app is crashing and so receiver is registered. Also check if its registered in manifest or in your activity

aused by: java.lang.IllegalArgumentException: Receiver not registered: null

You are creating a new (local) BroadcastReceiver in your onResume()

BroadcastReceiverListener receiver = new BroadcastReceiverListener();

but in your onPause() the receiver refers to the uninitialized instance variable which is null

unregisterReceiver(receiver); // receiver is null

So, to fix the issue just instantiate the receiver once in your onCreate() method

receiver = new BroadcastReceiverListener();

Then both onPause() and onResume() will un/register the same instance.

Receiver not registered

Try as follows...

    mRefreshReceiver = new BroadcastReceiver() {

@Override
public void onReceive(Context context, Intent intent) {

log("BroadcastIntent received in MainActivity");

}
}

In onResume()...

@Override
protected void onResume() {
super.onResume();

IntentFilter filter = new IntentFilter(DATA_REFRESHED_ACTION);
registerReceiver(mRefreshReceiver, filter);

}

In AsyncTask....

    Intent intent = new Intent(DATA_REFRESHED_ACTION);
sendOrderedBroadcast(intent, null, new BroadcastReceiver() {

@SuppressLint("NewApi")
@Override
public void onReceive(Context context, Intent intent) {

log("BroadcastIntent received in MainActivity");

}
}, null, Activity.RESULT_OK, null, null);
}

You can follow the below link...

Android sendOrderedBroadcast Example



Related Topics



Leave a reply



Submit