BroadcastReceiver with multiple filters or multiple BroadcastReceivers?

I have Android activity that should capture two different broadcasts. My current approach is to have one BroadcastReceiver inside an Activity and catch broadcasts with it:

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

I want to execute refresh() only if my activity is visible on the screen, but I want to catch INTENT_UPDATE and execute update() for the duration of the Activity, regardless of whether the activity is visible or not.

I did not find a way to unregister only one of the two filters that I registered on onCreate , so I use the flag to enable or disable the action that should be performed when the INTENT_REFRESH broadcast INTENT_REFRESH caught, depending on the status of the activity.

Question : is this the right approach?

Or, it would be better to have two separate BroadcastReceivers as follows:

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

And which one has the best performance?

+75
android android-intent broadcastreceiver
Feb 03 2018-12-12T00:
source share
2 answers

instead, you can provide two different intent filters:

filter for update only

 IntentFilter filterRefresh = new IntentFilter(Params.INTENT_REFRESH); 

filter to update and update

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

now you can switch between intent filters by registering and not registering what you want, but the implementation of your receiver will be the same

+139
Feb 03 2018-12-12T00:
source share

For each action, create an IntentFilter and register it.

 @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(); } } } 
+24
May 9 '13 at 7:34
source share



All Articles