gpt4 book ai didi

android - AsyncTask 文件下载时 ListView 中的进度条

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:52:17 26 4
gpt4 key购买 nike

我有一个奇怪的问题,每当我在网络浏览器上找到一个 MP3 文件时,我都会启动一个新的异步任务,并在每个异步任务的 ListView 中启动一个进度条。所以下载的数量可以超过 1 并且是同时的。但是现在,每当启动 Asynctask 时,ProgressBar 对所有 AsyncTask 的移动都相同,并且对于不同的 AsyncTask 没有不同,请指导我......

LISTVIEW WITH PROGRESS BAR

    public class CopyOfDownloadsListActivity extends ListActivity {
/** Called when the activity is first created. */

// static ArrayList<String> pthreads = new ArrayList<String>();
ImageView bt;
ProgressBar pb;
ListView allList;
TextView tv;
String fileName;
String mp3URL;
URL url2;
int filecount = 0;

private class DownloadFile extends AsyncTask<String, Integer, Void>{
MyCustomAdapter adapter;

int count = 0;
ProgressDialog dialog;
ProgressBar progressBar;
int myProgress;

@Override
protected Void doInBackground(String... u) {
try {
URL ul = new URL(u[0]);
Log.i("UI",ul.toString());
// int len = CopyOfMusicDownloader.mp3urls.size();
// URL url2 = new URL(CopyOfMusicDownloader.mp3urls.get(len-1));
HttpURLConnection c = (HttpURLConnection) ul.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();

int lengthOfFile = c.getContentLength();

String PATH = Environment.getExternalStorageDirectory()
+ "/download/";
Log.v("", "PATH: " + PATH);
File file = new File(PATH);
file.mkdirs();

fileName = "Track";
filecount++;

fileName = fileName + Integer.toString(filecount) + ".mp3";


File outputFile = new File(file, fileName);
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();

byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
myProgress = (int)((len1/lengthOfFile)*100);
myProgress = myProgress + myProgress;
Log.i("lengthOfFile", Integer.toString(lengthOfFile));
Log.i("My Progress", Integer.toString(myProgress));
publishProgress(myProgress);
fos.write(buffer, 0, len1);
}
fos.close();
is.close();

}catch (IOException e) {
e.printStackTrace();
}
return null;
}

protected void onPostExecute() {
}

@Override
protected void onPreExecute() {
adapter = new MyCustomAdapter(CopyOfDownloadsListActivity.this, R.layout.row, CopyOfMusicDownloader.mp3urls);
setListAdapter(adapter);
}



@Override
protected void onProgressUpdate(Integer... values) {
Log.i("Value", values[0].toString());
count++;
adapter.notifyDataSetChanged();
}


public class MyCustomAdapter extends ArrayAdapter<String> {
public MyCustomAdapter(Context context, int textViewResourceId, ArrayList<String> pthreads) {
super(context, textViewResourceId, pthreads);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.row, parent, false);
bt =(ImageView)row.findViewById(R.id.cancel_btn);
tv =(TextView)row.findViewById(R.id.filetext);
pb = (ProgressBar)row.findViewById(R.id.progressbar_Horizontal);
pb.setProgress(count);
return row;
}
}
}

这是 AsyncTask 的开始,它的 onCreate new DownloadFile().execute(url2.toString());

最佳答案

此外,请阅读此处关于 execute() 的注释:

http://developer.android.com/reference/android/os/AsyncTask.html#execute%28Params...%29

在 Honeycomb 之后,将只有一个线程运行所有 ASyncTasks,因此您可能无法同时运行多个线程。在第一个之后,其余的将排队并且在第一个完成之前根本不会执行。您可能需要在线程中执行每个下载并使用单个 ASyncTask 来监视所有下载。 (您也可以从线程进行监控,但您需要采取额外的步骤来安全地将更新发布到 UI 线程上的 UI)。

关于android - AsyncTask 文件下载时 ListView 中的进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7096903/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com