Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Registering a broadcast receiver for two intents?

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?

like image 764
Donal Rafferty Avatar asked Feb 23 '10 11:02

Donal Rafferty


People also ask

How many ways can you register a broadcast receiver?

A BroadcastReceiver can be registered in two ways. By defining it in the AndroidManifest. xml file as shown below.

How do I register a broadcast receiver dynamically?

If the receiving class is not registered using in its manifest, you can dynamically instantiate and register a receiver by calling Context. registerReceiver(). Take a look at registerReceiver (BroadcastReceiver receiver, IntentFilter filter) for more info.

How do you declare a broadcast receiver in manifest?

There are two ways to make a broadcast receiver known to the system: One is declare it in the manifest file with this element. The other is to create the receiver dynamically in code and register it with the Context. registerReceiver() method.


1 Answers

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

IntentFilter filter = new IntentFilter(SIPEngine.SIP_REGISTERED_INTENT);
filter.addAction(SIPEngine.SIP_UNREGISTERED_INTENT);
context.registerReceiver(sipRegistrationListener, filter);
like image 80
Christopher Orr Avatar answered Oct 02 '22 23:10

Christopher Orr