Android - 为两个意图注册广播接收器?

时间:2010-02-23 11:08:33

标签: android android-intent broadcastreceiver intentfilter

我想知道是否可以注册广播接收器以接收两个意图?

我的代码如下:

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));

每次发送时我都会获得注册意图,但是当我发送它时,我从未获得UNREGISTERED Intent。

我应该为UNREGISTERED Intent设置另一个广播接收器吗?

1 个答案:

答案 0 :(得分:25)

请勿创建IntentFilter内联,然后使用addAction方法添加UNREGISTERED操作,即:

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