btnDownload.setOnClickListener(newOnClickListener() { publicvoid onClick(View v) { String url = datas.get(position).get("url"); Async asyncTask =null; // 下载renwu booleanisHas = false; // 判断当前要下载的这个连接是否已经正在进行,如果正在进行就阻止在此启动一个下载任务 for(String urlString : AppConstants.listUrl) { if(url.equalsIgnoreCase(urlString)) { isHas =true; break; } } // 如果这个连接的下载任务还没有开始,就创建一个新的下载任务启动下载,并这个下载任务加到下载列表中 if(isHas ==false) { asyncTask =new Async(); // 创建新异步 asyncTask.setDataMap(datas.get(position)); asyncTask.setContext(context); AppConstants.mapTask.put(url, asyncTask); // 当调用AsyncTask的方法execute时,就会去自动调用doInBackground方法 asyncTask.executeOnExecutor(Executors.newCachedThreadPool(), url); } } });
先来看看列表页都有些什么
packagecom.test.muldownloadtest; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.Button; import android.widget.LinearLayout; import android.widget.ScrollView; public class DownloadManagerActivity extendsActivity implementsOnClickListener { privateScrollView scDownload; privateLinearLayout llDownloadLayout; @Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);this.requestWindowFeature(Window.FEATURE_NO_TITLE) ;
this.setContentView(R.layout.download_manager_layout);initView();}@Overrideprotectedvoid onResume() { super.onResume(); refreshItemView();}privatevoid initView(){ Button btnGoback = (Button)this.findViewById(R.id.btnGoback); btnGoback.setOnClickListener(this); scDownload = (ScrollView)this.findViewById(R.id.svDownload); scDownload.setSmoothScrollingEnabled(true); llDownloadLayout = (LinearLayout)this.findViewById(R.id.llDownloadLyout);}/*** 列表中的每一项*/privatevoid refreshItemView(){for (int i = 0; i < AppConstants.listUrl.size(); i++) { DownloadItemView downloadItemView =new DownloadItemView(this, AppConstants.listUrl.get(i), i); downloadItemView.setId(i); downloadItemView.setTag("downloadItemView_"+i); llDownloadLayout.addView(downloadItemView);}}public void onClick(View v) {switch (v.getId()) { case R.id.btnExit: this.finish(); break; case R.id.btnGoback: this.finish(); break; default: break; } }} 很简单,一个ScrollView,在这个ScrollView中在内嵌一个LinearLayout,用这个LinearLayout来存储每一个列表项。其实列表项很简单,最基本只要三个控件就行了——ProgressBar、TextView、Button。一个是进度条,一个显示百分比,一个用来暂停/继续,偷个懒,这个布局文件就不列出来了,咱就看看这个Button都干嘛了。ublic void onClick(View v) { switch (v。getId()) { case R。id。btnPauseOrResume: String btnTag = (String) btnPauseOrResume。getTag(); if (btnTag。equals("pause")) { resumeDownload(); }else if (btnTag。equals("resume")) { pauseDownload(); } break; default: break; } } privatevoid pauseDownload(){ btnPauseOrResume。setTag("pause"); btnPauseOrResume。setText(R。string。download_resume); Async pauseTask =null; // 判断当前被停止的这个任务在任务列表中是否存在,如果存在就暂停 if (AppConstants。linkedMapDownloading。containsKey(urlString)) { pauseTask = AppConstants。linkedMapDownloading。get(urlString); if (pauseTask != null) { pauseTask。pause(); } } } privatevoid resumeDownload(){ btnPauseOrResume。setTag("resume"); btnPauseOrResume。setText(R。string。download_pause); Async continueTask =null; // 判断当前被停止的这个任务在任务列表中是否存在,如果存在就继续 if (AppConstants。linkedMapDownloading。containsKey(urlString)) { continueTask = AppConstants。linkedMapDownloading。get(urlString); if (continueTask != null) { continueTask。continued(); } } handler。postDelayed(runnable,1000); }
简单吧,就是判断一下当前按钮的Tag,然后根据Tag的值,来判断是继续下载,还是暂停下载。而这个暂停还是继续,其实只是修改下Async中的暂停标记的值,即paused是true还是false。