[Solved] Download data from internet in background and concurrently share them among all activities. How?


Using the structure recommended by Nick Cardoso but with many changes to meet my case, i managed to solve the problem. here it is:

class A extends Service {
ArrayList arrayList = new ArrayList();
MyApplication app;
void foo(){
   new Thread (new Runnable (){        
    @Override
    public void run() {
       app = (MyApplication)getApplication();
       While(true){
       //get elements from network and put them in arrayList
       app.synchronisedAddCollection(arrayList);            
       LocalBroadcastManager.getInstance(this).sendBroadcast(mediaIntent);
    }
    }  
  }).start();                        
 }
}

And here is my Application class:

public class MyApplication extends Application {
List<HashMap<String, String>> myArrayList = new ArrayList<HashMap<String, String>>();

@Override
public void onCreate() {
    super.onCreate();        
}

public void synchronisedAddCollection(ArrayList<HashMap<String, String>> arrayList) {
    myArrayList.addAll(arrayList); 
}

public ArrayList<HashMap<String, String>> getArrayList(){
    return (ArrayList<HashMap<String, String>>) myArrayList;
}
} 

Here is the activity which needs to access the shared arraylist

 class B extends Activity {
  MyApplication app;
 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    startService(new Intent(getApplicationContext(), MyService.class); 
    LocalBroadcastManager.getInstance(this).registerReceiver(lbr,
              new IntentFilter("mediaIntent"));
 }
 private BroadcastReceiver lbr = new BroadcastReceiver() {
      @Override
      public void onReceive(Context context, Intent intent) {
          app = (MyApplication)getApplication();
          //now i have access to the app arrayList
          System.out.println(app.myArrayList.size());     
      }
 }
 };
}

Do not forget to register MyApplication and MyService in manifest.

solved Download data from internet in background and concurrently share them among all activities. How?