KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have an Android Activity that needs to catch two different broadcasts. My current approach is to have a single BroadcastReceiver within the Activity and catch both the 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() during the entire lifetime of the Activity, regardl
Tags (comma-separated)
Save Edits
Cancel