带有多个过滤器或多个 BroadcastReceiver 的 BroadcastReceiver? ?

我有一个 Android 活动,需要捕捉两个不同的广播。我目前的方法是在 Activity 中有一个单独的 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();
}
}
}
}

只有当活动在屏幕上可见时,我才想执行 refresh(),但是我想捕获 INTENT_UPDATE并在活动的整个生命周期中执行 update(),而不管活动是否可见。

我没有找到任何方法只注销我在 onCreate中注册的两个过滤器中的一个,所以我使用一个标志来启用或禁用当 INTENT_REFRESH广播被捕获时要执行的操作,这取决于活动的状态。

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

或者,是否应该有两个独立的 BroadcastReceiver,如下所示:

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

哪一个的表现更好呢?

88096 次浏览

相反,您可以提供两个不同的意图过滤器:

只用于刷新的过滤器

IntentFilter filterRefresh = new IntentFilter(Params.INTENT_REFRESH);

用于刷新和更新的过滤器

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

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

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

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


}

使用 KOTLIN 你可以做到这一点:

  broadcastReceiver = NukeBroadcastReceiver()
registerReceiver(broadcastReceiver, IntentFilter().apply {
addAction(ACTION_DESTROY_EVERYTHING)
addAction(ACTION_RESTART_WORLD)
})