安卓:非同步线程需要吗? [英] Android: Asynch Thread Needed?

查看:112
本文介绍了安卓:非同步线程需要吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个应用程序,包括从文本文件,这些文件在Assets文件夹中读取数据。对于每一个文件,它存储在一个单独的的ArrayList 中的数据。该文件被读入一个又一个的活动的onCreate()方法。所有文本文件的总和1.8 MB,并在模拟器上它目前需要12秒钟的活动来加载。该应用程序在模拟器上不会崩溃(它只是需要大约12秒)。

I am creating an app that involves reading data from text files that are in the Assets folder. For each file, it stores the data in a separate ArrayList. The files are read in one after another in the onCreate() method of the activity. All of the text files combined total 1.8 MB and on the emulator it currently takes 12 seconds for the activity to load. The app does not crash on the emulator (it just takes approx 12 seconds).

我看了一下异步线程,但我从来没有需要他们过去。我打算有某种消息或进度条通知的活动实际上是在加载并没有崩溃的用户。

I have read about asynchronous threads, but I have never had a need for them in the past. I was planning on having some sort of message or progress bar to notify the user that the activity is in fact loading and has not crashed.

所以我的问题是:即使装载活动时,应用程序不会崩溃,我应该还是把这些文件的读取上一个异步或不同的线程?如果是这样,我怎么会去正确地做这件事? (我以前从来没有这样做。)

So my question is: even though the app does not crash when loading the activity, should I still put the reading of the files on an asynchronous or different thread? If so, how would I go about doing it properly? (I have never done it before.)

下面是一个示例code与文本文件的阅读:

Here is sample code with the reading of the text files:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_name);

    populateArrayLists();

}

public void populateArrayLists() {
    BufferedReader br = null;
    try {
        br = new BufferedReader(new InputStreamReader(getAssets().open(
                "text1.txt")));
        String text;
        while ((word = br.readLine()) != null) {
            ArrayList1.add(text);
        }                           
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            br.close(); // stop reading
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }


    br = null;
    try {
        br = new BufferedReader(new InputStreamReader(getAssets().open(
                "text2.txt")));

    // the same process is duplicated for 1-15
    // it may not be the best or most efficient way but it works

任何帮助将是AP preciated。

Any help would be appreciated.

推荐答案

是的,你需要一个后台线程这一点。该装载机API 可能是你最简单的选择。

Yes, you'll need a background thread for this. The Loader api may be your easiest bet.

这将让你至少显示一个通知,并同时提供文件加载一些内容。甚至加载它们并逐渐显示数据,如果这是你在做什么。

This will allow you at least display a notice and offer some content while the files load. Maybe even load them and incrementally displaying the data, if that's what you're doing.

修改:装载机都在的兼容库。如果你不愿意添加的依赖,你可以随时退回到的AsyncTask ,在这种情况下,你可以看看的这个

Edit: Loaders are a 3.0 feature available in the compatibility library. If you're not willing to add the dependency, you can always fall back to AsyncTask, in which case you could take a look at this.

这篇关于安卓:非同步线程需要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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