Android 从父活动继承BroadcastReceiver

Android 从父活动继承BroadcastReceiver,android,android-intent,broadcastreceiver,Android,Android Intent,Broadcastreceiver,我有三个活动:BaseActivity、ActivityA、ActivityB,其中ActivityA和B扩展了BaseActivity。在ActivityA和B中,我都有一个BroadcastReceiver来处理一些意图。ActivityA和B中都有一些相同的意图,它们当前由相同的代码处理。因为这违反了DRY原则,所以我想在父类(BaseActivity)中处理ActivityA和B中的公共意图。有可能吗 这是代码。在ActivityA和B中,登录成功的意图是相同的。而帐户更新和日志更新的意

我有三个活动:BaseActivity、ActivityA、ActivityB,其中ActivityA和B扩展了BaseActivity。在ActivityA和B中,我都有一个BroadcastReceiver来处理一些意图。ActivityA和B中都有一些相同的意图,它们当前由相同的代码处理。因为这违反了DRY原则,所以我想在父类(BaseActivity)中处理ActivityA和B中的公共意图。有可能吗

这是代码。在ActivityA和B中,登录成功的意图是相同的。而帐户更新和日志更新的意图是特定于类的

public abstract class BaseActivity extends RoboActivity implements Handler.Callback {

}


public class ActivityA extends BaseActivity {

    private final BroadcastReceiver intentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Constants.INTENTS.ACCOUNT_UPDATE)) {
            updateGUI();
        }

        if (intent.getAction().equals(Constants.INTENTS.LOGIN_SUCCESS)) {
            updateGUI();
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        // Register which actions to listen to
        IntentFilter intentFilter = new IntentFilter(Constants.INTENTS.ACCOUNT_UPDATE);
        intentFilter.addAction(Constants.INTENTS.LOGIN_SUCCESS);
        LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(intentReceiver, intentFilter);
    }
}

public class ActivityB extends BaseActivity {

    private final BroadcastReceiver intentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals(Constants.INTENTS.JOURNAL_UPDATE)){
            updateGUI();
        }

        if (intent.getAction().equals(Constants.INTENTS.LOGIN_SUCCESS)) {
            updateGUI();
        }
        }
    };

     @Override
    protected void onResume() {
        super.onResume();
        // Register which actions to listen to
        IntentFilter intentFilter = new IntentFilter(Constants.INTENTS.JOURNAL_UPDATE);
        intentFilter.addAction(Constants.INTENTS.LOGIN_SUCCESS);
        LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(intentReceiver, intentFilter);
    }
}

阅读以下链接中的安全部分。我想这就是你要找的。

是的,如果我正确理解了你的问题,就有可能做到你所要求的

我知道这是一个老生常谈的问题,但因为还没有其他人提供任何代码示例,所以我想提供一个可能的解决方案。我不会说这个例子是“最佳实践”,因为我不确定它是否是,但这是目前我帮助工作的一个大型项目中实现的,所以我知道它是有效的,而且它似乎满足了您对“应用程序内消息”的要求。为了保持例子的简洁性,如果您只是尝试将其复制并粘贴到项目中,代码将不会运行,但希望它能充分说明我试图表达的想法,使之有意义

基本活动包含您的广播接收器以及它们在接收各自消息时调用的方法。如果在扩展ExampleBase的类中重写ExampleBase.onBroadcastReceived()方法,则当收到帐户更新或日志更新消息时,每个类都可以处理自己的特定任务,并且可以在基本活动的方法中处理登录成功消息或任何其他共享消息

助手类:

public class BroadcastUtility {
    public static LocalBroadcastManager Manager(Context context) {
        return LocalBroadcastManager.getInstance(context);
    }

    public static void Register(Application context, BroadcastReceiver receiver, String intentName) {
        Manager(context).registerReceiver(receiver, new IntentFilter(intentName));
    }

    public static void Unregister(Application context, BroadcastReceiver receiver) {
        Manager(context).unregisterReceiver(receiver);
    }
}

public class Constants {
    // Your constants
    public class INTENTS {
        public static final String ACCOUNT_UPDATE = "ACCOUNT_UPDATE";
        public static final String JOURNAL_UPDATE = "JOURNAL_UPDATE";
        public static final String LOGIN_SUCCESS = "LOGIN_SUCCESS";
    }
}
基类:

public class ExampleBase extends FragmentActivity {
    private BroadcastReceiver mLoginSuccess = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // On receipt of the "Login Success" broadcast, call the ExampleBase.onBroadcastReceived(Intent) method
            onBroadcastReceived(intent);
        }
    };

    private BroadcastReceiver mAccountUpdate = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // On receipt of the "Account Update" broadcast, call the ExampleBase.onBroadcastReceived(Intent) method
            onBroadcastReceived(intent);
        }
    };

    private BroadcastReceiver mJournalUpdate = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // On receipt of the "Journal Update" broadcast, call the ExampleBase.onBroadcastReceived(Intent) method
            onBroadcastReceived(intent);
        }
    };

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        // Register the Broadcast Receivers on create
        BroadcastUtility.Register(getApplication(), mAccountUpdate, Constants.INTENTS.ACCOUNT_UPDATE);
        BroadcastUtility.Register(getApplication(), mJournalUpdate, Constants.INTENTS.JOURNAL_UPDATE);
        BroadcastUtility.Register(getApplication(), mLoginSuccess, Constants.INTENTS.LOGIN_SUCCESS);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // Unregister the Broadcast Receivers on destroy
        BroadcastUtility.Unregister(getApplication(), mAccountUpdate);
        BroadcastUtility.Unregister(getApplication(), mJournalUpdate);
        BroadcastUtility.Unregister(getApplication(), mLoginSuccess);
    }

    protected void onBroadcastReceived(Intent intent) {
        // Actions common to all activities extending this one can be done here in ExampleBase
        if (intent.getAction().equals(Constants.INTENTS.LOGIN_SUCCESS)) {
            // Do ExampleBase stuff
        }
    }
}
扩展基的类:

public class ExampleA extends ExampleBase {
    @Override
    protected void onBroadcastReceived(Intent intent) {
        super.onBroadcastReceived(intent);
        // Actions specific to ExampleA can be done here
        if (intent.getAction().equals(Constants.INTENTS.ACCOUNT_UPDATE)) {
            // Do ExampleA stuff
        }

        if (intent.getAction().equals(Constants.INTENTS.JOURNAL_UPDATE)) {
            // Do ExampleA stuff
        }
    }
}

public class ExampleB extends ExampleBase {
    @Override
    protected void onBroadcastReceived(Intent intent) {
        super.onBroadcastReceived(intent);
        // Actions specific to ExampleA can be done here
        if (intent.getAction().equals(Constants.INTENTS.ACCOUNT_UPDATE)) {
            // Do ExampleB stuff
        }

        if (intent.getAction().equals(Constants.INTENTS.JOURNAL_UPDATE)) {
            // Do ExampleB stuff
        }
    }
}

我不明白为什么这与我的问题有关。您能详细说明一下吗?@v4r如果两个广播接收器接收相同的广播,那么使用优先级,您可以继续检查哪个接收器首先处理广播。安全部分讨论跨应用程序安全。在我的例子中,消息在同一个应用程序中发送和接收,但活动不同。