具有多个filter或多个BroadcastReceivers的BroadcastReceiver?

我有一个Android活动,需要抓住两个不同的广播。 我目前的做法是在活动中有一个单一的BroadcastReceiver ,并用它来捕获广播:

 public class MyActivity extends Activity { private MyActivity.BroadcastListener mBroadcastListener; private boolean mIsActivityPaused = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mylayout); // Create the broadcast listener and register the filters mIsActivityPaused = false; mBroadcastListener = new BroadcastListener(); IntentFilter filter = new IntentFilter(); filter.addAction(Params.INTENT_REFRESH); filter.addAction(Params.INTENT_UPDATE); registerReceiver(mBroadcastListener, filter); } @Override protected void onResume() { super.onResume(); mIsActivityPaused = false; } @Override protected void onPause() { super.onPause(); mIsActivityPaused = true; } @Override protected void onDestroy() { unregisterReceiver(mBroadcastListener); super.onDestroy(); } private void refresh() { // refresh } private void update() { // update } private class BroadcastListener extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Params.INTENT_REFRESH && !mIsActivityPaused)) { refresh(); } else if (intent.getAction().equals(Params.INTENT_UPDATE)) { update(); } } } } 

我只想在屏幕上显示我的Activity时执行refresh() ,但是我想捕获INTENT_UPDATE并在Activity的整个生命周期中执行update() ,而不pipeActivity是否可见。

我没有find任何方法来注销我在onCreate注册的两个filter中的一个,所以我使用一个标志来启用或禁用在捕获INTENT_REFRESH广播时执行的操作,具体取决于Activity的状态。

问题是 :这是正确的方法吗?

或者,如果有两个独立的BroadcastReceivers是更好的,如下所示:

 public class MyActivity extends Activity { private MyActivity.BroadcastListenerRefresh mBroadcastListenerRefresh; private MyActivity.BroadcastListenerUpdate mBroadcastListenerUpdate; private boolean mIsBroadcastListenerRefreshRegistered = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Create the broadcast listeners mBroadcastListenerRefresh = new BroadcastListenerRefresh(); mBroadcastListenerUpdate = new BroadcastListenerUpdate(); registerReceiver(mBroadcastListenerRefresh, new IntentFilter(Params.INTENT_REFRESH)); registerReceiver(mBroadcastListenerUpdate, new IntentFilter(Params.INTENT_UPDATE)); } @Override protected void onResume() { super.onResume(); if (mBroadcastListenerRefresh != null && !mIsBroadcastListenerRefreshRegistered) { registerReceiver(mBroadcastListenerRefresh, new IntentFilter(Params.INTENT_REFRESH)); mIsBroadcastListenerRefreshRegistered = true; } } @Override protected void onPause() { super.onPause(); if (mBroadcastListenerRefresh != null && mIsBroadcastListenerRefreshRegistered) { unregisterReceiver(mBroadcastListenerRefresh); mIsBroadcastListenerRefreshRegistered = false; } } @Override protected void onDestroy() { unregisterReceiver(mBroadcastListenerRefresh); unregisterReceiver(mBroadcastListenerUpdate); super.onDestroy(); } private void refresh() { // refresh } private void update() { // update } private class BroadcastListenerRefresh extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Params.INTENT_REFRESH)) { refresh(); } } } private class BroadcastListenerUpdate extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Params.INTENT_UPDATE)) { update(); } } } } 

哪一个有更好的performance?

相反,您可以提供两种不同的意图filter:

filter只能刷新

 IntentFilter filterRefresh = new IntentFilter(Params.INTENT_REFRESH); 

filter进行刷新和更新

 IntentFilter filterRefreshUpdate = new IntentFilter(); filterRefreshUpdate.addAction(Params.INTENT_REFRESH); filterRefreshUpdate.addAction(Params.INTENT_UPDATE); 

现在,您可以通过注册和取消注册所需的filter来在意向filter之间切换,但是您的接收器的实现将是相同的

对于每个操作,创buildIntentFilter并注册它。

 @Override protected void onResume() { super.onResume(); BroadcastListener receiver = new BroadcastListener(); // Register the filter for listening broadcast. IntentFilter filterRefresh = new IntentFilter(Params.INTENT_REFRESH); IntentFilter filterUpdate = new IntentFilter(Params.INTENT_UPDATE); registerReceiver(receiver, filterRefresh); registerReceiver(receiver, filterUpdate); } private class BroadcastListener extends BroadcastReceiver { public void onReceive(Context ctx, Intent intent) { if (intent.getAction().equals(Params.INTENT_UPDATE)) { update(); } else if(intent.getAction().equals(Params.INTENT_REFRESH)) { refresh(); } } }