我想在listview中使用下载管理器显示下载进度条。我尝试下面的代码它不能按我的意愿工作。单击listview项目,我可以下载多个文件。我希望该列表项的所有文件的单个进度条单击以显示在该列表视图项目进度对话框中。
FileDownloader.java
public class FileDownloader {
private DownloadManager downloadManager;
private long downloadReferenceId;
ArrayList<Long> ReferenceIdlist = new ArrayList<>();
private Context context;
DBHandler dbHandler;
OfferingEntries of = new OfferingEntries();
public FileDownloader(Context context) {
this.context = context;
dbHandler = new DBHandler(context);
}
public void DownloadData(ArrayList<String> filepaths, final ProgressBar view) {
for (int i = 0; i < filepaths.size(); i++) {
downloadManager = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);
String urlString = "http://192.168.1.55/...";
urlString = urlString.replaceAll(" ", "%20");
Uri Download_Uri = Uri.parse(urlString);
DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI|DownloadManager.Request.NETWORK_MOBILE);
request.setAllowedOverRoaming(false);
request.setVisibleInDownloadsUi(false);
request.setTitle("Data:" + filepaths.get(i));
request.setDescription("Downloading " + filepaths.get(i));
request.setDestinationInExternalFilesDir(context, Environment.DIRECTORY_DOWNLOADS + "/" + "myfiles" , filepaths.get(i));
downloadReferenceId = downloadManager.enqueue(request);
ReferenceIdlist.add(downloadReferenceId);
new Thread(new Runnable() {
@Override
public void run() {
boolean downloading = true;
while (downloading) {
DownloadManager.Query q = new DownloadManager.Query();
q.setFilterById(downloadReferenceId); //filter by id which you have receieved when reqesting download from download manager
Cursor cursor = downloadManager.query(q);
cursor.moveToFirst();
int bytes_downloaded = cursor.getInt(cursor
.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
int bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
downloading = false;
}
final int dl_progress = (int) ((bytes_downloaded * 100l) / bytes_total);
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
view.setProgress((int) dl_progress);
}
});
cursor.close();
}
}
}).start();
}
}
}
ListView点击代码
lesson_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String sel_lesson_id = ((TextView) view.findViewById(R.id.lesson_id)).getText().toString();
ProgressBar v=(ProgressBar)view.findViewById(R.id.bar);
of.setLesson_id(sel_lesson_id);
String course_id=of.getCourse_id();
ArrayList<String> filepaths=dbHandler.getFiles(course_id,sel_lesson_id);
ArrayList<String> newfilepaths = (ArrayList<String>)filepaths.clone();
File extStore = getActivity().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
for(int i = 0; i < filepaths.size(); i++) {
File myFile = new File(extStore.getAbsolutePath() + "/" + of.getOffering_id() + "/" + of.getLesson_id() + "/" + filepaths.get(i));
if (myFile.exists()) {
newfilepaths.remove(filepaths.get(i));
}
}
if (newfilepaths.size() == 0) {
/*Fragment fragment = null;
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
fragment = new PdfReader();
fragmentManager.beginTransaction().addToBackStack(null).replace(R.id.flContent, fragment).commit();*/
LessonNavigator ln=new LessonNavigator(getActivity());
ln.navigate();
} else {
isInternetPresent = cd.isConnectingToInternet();
if (!isInternetPresent) {
Toast.makeText(getActivity(), "No internet connection!!", Toast.LENGTH_SHORT).show();
} else {
FileDownloader fd;
//view.setBackgroundColor(Color.GRAY);
//view.setClickable(false);
//view.setFocusable(false);
fd = new FileDownloader(getActivity());
fd.DownloadData(filepaths,v);
Toast.makeText(getActivity(), "Files are being downloaded...Please check the progress in notification bar", Toast.LENGTH_SHORT).show();
}
}
}
});