I will give you a general idea that you can apply everything you need (including from Thread or ThreadExecutor (instead of relying solely on AsyncTask);
you can directly use the library to handle events , where events are sent on a common "bus", and any class can register on the bus and listen to these events:
and for this I will refer to Otto, it works well and is very powerful: http://square.imtqy.com/otto/
otherwise you can implement it yourself using LocalBroadcastManager https://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html as follows:
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context) <to get a link to it.
onEventComplete: (happens in your stream or in AsyncTask
Intent i = new Intent("DOWNLOAD_COMPLETE");
then in your activity / fragment / view you create a receiver
BroadcastReceiver receiver = new BroadcastReceiver(){ @Override public void onReceive (Context context, Intent intent){
onStartListening:
lbm.registerReceiver(receiver, new IntentFilter("DOWNLOAD_COMPLETE"));
onStopListening:
lbm.unregisterReceiver(receiver);
then you MUST start and stop listening during onStart / onStop or onResume / onPause or onAttachedToWindow / onDetachedFromWindow (for views)
source share