Sayed Imteaj Sayed Imteaj - 1 month ago 8
Android Question

Android Activity : Periodically updates both activity text from same data source

I have 2 Activities, i) MainActivity ii)FavoriteActivity and a non-activity class RadioService. Both Activity hava a identical method - notifyShowBar(), it shows a text bar defined in showbar.xml@layout.

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

@Override
protected void onResume() {
super.onResume();
notifyShowBar();
}
public void notifyShowBar() {
....
textView.setText(revs.getMeta());
....
}
.....


FavoriteActivity.java

public class FavoriteActivity extends AppCompatActivity implements View.OnClickListener {

@Override
protected void onResume() {
super.onResume();
notifyShowBar();
}
public void notifyShowBar() {
....
textView.setText(revs.getMeta());
....
}
.....


Now when an activity calls RadioService, it creates a timertask, then it fetch info from server periodically, and shows bar in parent activity. So the bar shows updated info in every 15 seconds.

private class ProgressTask extends AsyncTask<String, Void, Boolean> {

@Override
protected void onPostExecute(final Boolean success) {
updateMeta();
....
}

public void updateMeta() {
timerAsync = new Timer();
timerTaskAsync = new TimerTask() {
@Override
public void run() {
((Activity)context).runOnUiThread(new Runnable() {
@Override public void run() {
// get and set meta data here and then call createShowBar
createShowBar();
}
});
}
};
timerAsync.schedule(timerTaskAsync, 0, 15000);
}

private void createShowBar() {
if (RadioService.inwhich == 2) {
((FavoriteActivity) context).notifyShowBar();
} else {
((MainActivity) context).notifyShowBar();
}
}
.....
}


Problem is, if I execute a button in MainActivity which calls RadioService, it shows bar and updates there periodically. But when I browse to FavoriteActivity, it shows bar but don't update periodically. How can I make it happen for both activity? Thanks.

Answer

Use a BroadcastReceiver. You can register one with an intent filter and then it will receive intents sent with sendBroadcast. If you only want to update while the app is visible, register the receiver onStart and unregister onStop.

public class FavoriteActivity extends AppCompatActivity {
    private static final String ACTION_UPDATE = "Something unique";

    private static final IntentFilter FILTER = new IntentFilter(ACTION_UPDATE);

    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // do your thing
        }
    };

    @Override
    protected void onStart() {
        super.onStart();
        registerReceiver(receiver, FILTER);
    }

    @Override
    protected void onStop() {
        super.onStop();
        unregisterReceiver(receiver);
    }
}

Send a broadcast with

Intent intent = new Intent(ACTION_UPDATE);
// add data to intent
sendBroadcast(intent);

Also, look at LocalBroadcastManager on a more secure way to do this.