从父Activity继承BroadcastReceiver

时间:2014-04-15 08:59:45

标签: android android-intent broadcastreceiver

我有三个活动:BaseActivity,ActivityA,ActivityB,其中ActivityA和B扩展了BaseActivity。在ActivityA和B中,我有一个BroadcastReceiver来处理一些意图。 ActivityA和B中有一些相同的意图,目前由相同的代码处理。由于这违反了DRY原则,我想在父类(BaseActivity)中处理ActivityA和B中的常见意图。有可能这样做吗?

以下是代码。 ActivityA和B中的目标LOGIN_SUCCESS相同。虽然ACCOUNT_UPDATE和JOURNAL_UPDATE意图特定于类。

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

2 个答案:

答案 0 :(得分:0)

通过以下链接阅读安全部分。我想这就是你要找的东西。 http://developer.android.com/reference/android/content/BroadcastReceiver.html

答案 1 :(得分:0)

是的,如果我正确理解了您的问题,则可以按照您的要求做。

我知道这是一个老问题了,但是由于没有人提供任何代码示例,因此我想提供一个可能的解决方案。我不确定该示例是“最佳实践”,因为不确定它是什么,但是目前已在我协助进行的大型项目中实施,因此我知道它是可行的,并且似乎可以满足您对“应用内应用”的要求讯息。”为了使示例简洁明了,如果您仅尝试将其复制并粘贴到项目中,则该代码将不会运行,但希望它能说明我正尽力传达出有意义的想法。

基本活动包含您的广播接收器,以及它们在接收各自的消息时都会调用的方法。如果您在扩展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
        }
    }
}