简体   繁体   中英

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?

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

IntentFilter filter = new IntentFilter(SIPEngine.SIP_REGISTERED_INTENT);
filter.addAction(SIPEngine.SIP_UNREGISTERED_INTENT);
context.registerReceiver(sipRegistrationListener, filter);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM