从互联网在后台下载数据,并同时所有活动中共享。怎么样? [英] Download data from internet in background and concurrently share them among all activities. How?

查看:211
本文介绍了从互联网在后台下载数据,并同时所有活动中共享。怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从互联网上下载的元素,并将它们添加到背景的ArrayList。 (下载可能需要几分钟。)

I need to download elements from internet and add them to an arraylist in the background. (The download may take a few minutes.)

有一个循环,其中整体的元素的一部分被下载每一次迭代,并添加到列表中。我需要不同的活动,能够有机会获得所需每当ArrayList中,无论下载(循环)正在进行或已完成。

There is a loop in which part of overall elements are downloaded each iteration and added to the list. I need different activities be able to have access to that arraylist whenever needed, no matter if the download (the loop) is in progress or finished.

这似乎是一个服务可以做到这一点,但我没有对如何任何想法。考虑下面的code,我怎么能做到这一点?

It seems a service can do this, but i don't have any idea on how. Considering the code below, how can i achieve this?

class A extends Service {
    void foo(){
    //uses a loop to get elements from internet 
    //then adds the elements to myArraylist in each loop
    }
}


class B extends Activity {
    //needs to have access to myArraylist asynchronously
}


class C extends Activity {
    //needs to have access to myArraylist asynchronously
}  

请注意,我需要下载过程中保持活动状态时,活动之间的用户切换。

Note that i need the download process stay active when user switches between activities.

推荐答案

使用由尼克·卡多佐但有许多变化,建议,以满足我的情况的结构,我设法解决这个问题。这里是:

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

下面是需要访问共享ArrayList中的活动

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

不要忘了在清单中注册所有MyApplication和为MyService。

Do not forget to register MyApplication and MyService in manifest.

这篇关于从互联网在后台下载数据,并同时所有活动中共享。怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆