为什么这个code需要很长的时间? [英] Why does this code take a long time?

查看:134
本文介绍了为什么这个code需要很长的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这下面code获取存储在SD卡中的所有歌曲。

I am using this following code for getting all songs stored in the sdcard.

<一个href=\"http://stackoverflow.com/a/12227047/2714061\">http://stackoverflow.com/a/12227047/2714061

那么为什么会发生这种code需要很长时间恢复的歌曲列表中。
我已经包括了这是从我的播放器的播放列表中的onCreate方法被调用函数这个code。结果
这是发生了什么。结果
1:当应用程序运行时,第一次在我的Andr​​oid pH值执行,播放列表没有任何显示,因此被认为是空的结果
2:实例 - >后好30秒,当我再次要求它立即返回所有歌曲播放列表搜索

Well why does this code take so long to return this list of songs. I have included this code in a function which is called from the oncreate method in my player's playlist.
This is what happens.
1: When the application runs is executed for the first time on my android ph, the playlist has nothing to show, and hence is seen empty.
2: Well after for instance-> 30sec when I again call for the playlist it returns instantly all the songs.

因此​​,给人的感觉就好像这件事情需要时间来执行呢?结果
为什么会这样?结果

Hence, giving the feel as though this thing takes time to execute?
Why does this happen?

推荐答案

如何使用异步任务,读取一个文件或下载的东西,需要需要用户等待的时候,你必须考虑使用异步任务如此目的

How about using an asynchronous task, reading a file or downloading something, takes time that requires the user to wait, you must think of using an Asynchronous task for this purpose,

1:从开发者参考我们有:
AsyncTask的正确实现和易于使用的用户界面线程。此类允许进行后台操作并发布在UI线程上的结果,而无需操作线程和/或处理程序。 http://developer.android.com/reference/android/os/AsyncTask.html

这是异步任务由3泛型类型定义,所谓PARAMS,进展及结果,以及4个步骤,呼吁preExecute,doInBackground,onProgressUpdate和onPostExecute。

An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called onPreExecute, doInBackground, onProgressUpdate and onPostExecute.

2 所以,你可能有一个异步任务类:

2: So you may include an Async task class as:

 class DoBackgroundTask extends AsyncTask<URL, Void, ArrayList> {
           /*
             URL is the file directory or URL to be fetched, remember we can pass an array of URLs, 
            Void is simple void for the progress parameter, you may change it to Integer or Double if you also want to do something on progress,
            Arraylist is the type of object returned by doInBackground() method.

           */
    @Override
    protected ArrayList doInBackground(URL... url) {
     //Do your background work here
     //i.e. fetch your file list here

              return fileList; // return your fileList as an ArrayList

    }

    protected void onPostExecute(ArrayList result) {

    //Do updates on GUI here
     //i.e. fetch your file list from result and show on GUI

    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        // Do something on progress update
    }

}
//Meanwhile, you may show a progressbar while the files load, or are fetched.

此的AsyncTask可以通过调用其execute方法并传递参数给它被称为onCreate方法:

This AsyncTask can be called from you onCreate method by calling its execute method and passing the arguments to it:

 new DoBackgroundTask().execute(URL);

3 ,最后,还有约AsyncTasks一个很不错的教程在这里的 http://www.vogella.com/articles/AndroidBackgroundProcessing/article.html

3: And at last, there is also a very nice tutorial about AsyncTasks here, http://www.vogella.com/articles/AndroidBackgroundProcessing/article.html

这篇关于为什么这个code需要很长的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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