AsyncTask 文件下载时列表视图中的进度条
我遇到了一个奇怪的问题,每当我在网络浏览器上找到 MP3 文件时,就会启动一个新的异步任务,并在每个异步任务的列表视图中启动一个进度条。所以下载数量可以多于1并且同时进行。但现在每当启动 Asynctask 时,ProgressBar 的移动方式对于所有 AsyncTask 都是相同的,并且对于不同的 AsyncTask 没有不同,请指导我......
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());
I am having a strange issue, I have a new asynctask launced whenever i find an MP3 file on webbrowser, and lauch a progress bar in the listview for each AsyncTask. So number of downloads can be more than 1 and simultaneous. But now whenever Asynctask is launced ProgressBar moves same for all and not different for different AsyncTask, Plz guide me.......
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;
}
}
}
This is the AsyncTask start, and its onCreate
new DownloadFile().execute(url2.toString());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
另外,请阅读有关execute()的注释:
http://developer.android.com/reference/android/os/AsyncTask.html#execute%28Params...%29
Honeycomb 之后只有一个线程运行所有 ASyncTasks,所以你可能不能同时运行多个。在第一个之后,其余的将排队并且在第一个完成之前根本不会执行。您可能需要在线程中执行每个下载,并使用单个 ASyncTask 来监视所有下载。 (您也可以从线程进行监视,但需要采取额外的步骤才能安全地将更新发布到 UI 线程上的 UI)。
Also, read the notes on execute() here:
http://developer.android.com/reference/android/os/AsyncTask.html#execute%28Params...%29
After Honeycomb there will be only one thread running all ASyncTasks so you probably can't run more than one simultaneously. After the first, the rest will queue up and won't execute at all until the first one finishes. You'll probably need to perform each download in a Thread and use a single ASyncTask to monitor all of them. (You can monitor from a Thread too but you need to take extra steps to safely post updates to the UI on the UI thread).
您的问题是这个
static ProgressBar pb;
您不能拥有单个静态引用并期望控制多个进度栏。将ProgressBar完全封装在AsyncTask中,使其成为实例变量。
编辑
在 onProgressUpdate 中,您需要更改 ProgressBar 的进度。您不需要为每一行都配备一个适配器,这很浪费。
你也从来没有分配一个值progressBar,你一直评估
pb
,摆脱那个变量!在预执行中,您不断重新分配列表的适配器。你需要大量的修改。创建管理列表视图的单个适配器。每行都可以有一个 AsyncTask,它维护自己的进度条、图像视图和文本视图。
Your issue is this
static ProgressBar pb;
You can't have a single static reference and expect to control multiple progress bars. Fully encapsulate the ProgressBar inside the AsyncTask, make it a instance variable.
EDIT
In onProgressUpdate you need to change the progress of the ProgressBar. You don't need an adapter for every row, its wasteful.
You also never assign a value progressBar, you keep assessing
pb
, get rid of that variable! In you PreExecute you keep reassigning the List's adapter.You need A LOT of modifications. Create a single adapter that manages the listview. Each row can have an AsyncTask that maintains its own progress bar, image view and text view.