views:

1532

answers:

1

Hi,

I was wondering is it possible to register a broadcast receiver to receive two intents?

My code is as follows:

sipRegistrationListener = new BroadcastReceiver(){

@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction(); 

if(SIPEngine.SIP_REGISTERED_INTENT.equals(action)){
    Log.d("SETTINGS ", "Got REGISTERED action");
}   

if(SIPEngine.SIP_UNREGISTERED_INTENT.equals(action)){
    Log.d("SETTINGS ", "Got UNREGISTERED action");
}   
}
};
context.registerReceiver(sipRegistrationListener, new IntentFilter(SIPEngine.SIP_REGISTERED_INTENT));
context.registerReceiver(sipRegistrationListener, new IntentFilter(SIPEngine.SIP_UNREGISTERED_INTENT));

I get the REGISTERED Intent everytime I send it but I never get the UNREGISTERED Intent when I send it.

Should I set up another Broadcast receiver for the UNREGISTERED Intent?

+2  A: 

Don't create your IntentFilter inline, then use the addAction method to add the UNREGISTERED action, i.e.:

IntentFilter filter = IntentFilter(SIPEngine.SIP_REGISTERED_INTENT);
filter.addAction(SIPEngine.SIP_UNREGISTERED_INTENT);
context.registerReceiver(sipRegistrationListener, filter);
Christopher
Can you do that in an XML?
Macarse
Thanks for that Christopher, I have now come across another problem, I cant seem to send the intent from the onDestroy() method, the intent never gets sent. Is this a limitation of the onDestroy() method or just bad programming on my part? :)
Donal Rafferty
Macarse: Yes, you just include two <action> tags in your <intent-filter>.Donal: Is the `BroadcastReceiver` that you're registering to handle this in the same `Activity` in which you're calling `onDestroy()`? If so, your BR is likely being torn down before the broadcast `Intent` can reach it. Otherwise, I'm not aware of any restriction on when you can send broadcasts -- not that I've explicitly tried it from an `onDestroy` method...
Christopher
Thanks Christopher, that was it indeed and I have sorted it out now
Donal Rafferty