我在我的应用中使用Android下载管理器。如何在活动中显示进度条中的百分比时,如何访问下载进度(如通知中的下载百分比)?
这是我下载文件的方法:
////////////////////////////////////////////////////////////
//================== btn download set on click listner ===========
////////////////////////////////////////////////////////////
holder.btnDownload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
Uri downloadUri = Uri.parse(questionha.get(position).getQuestionDownLink());
if (!Uri.EMPTY.equals(downloadUri)) {
Request request = new Request(downloadUri);
request.setTitle(questionha.get(position).getQuestionTitle());
request.setDescription("Downloading ....");
request.setDestinationInExternalFilesDir(v.getContext(), "downloaded", questionha.get(position).getQuestionDownFileName());
enqueue = dm.enqueue(request);
}else {
Log.i("Download manager","Download manager url is null !");
}
}
});
答案 0 :(得分:2)
在Android下载管理器中搜索文档后,我找到了这个解决方案:
String urlDownload = "something";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(urlDownload));
request.setDescription("This is Desc");
request.setTitle("download picsArt");
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "teste.zip");
final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
final long downloadId = manager.enqueue(request);
final ProgressBar mProgressBar = (ProgressBar) findViewById(R.id.progressBar1);
new Thread(new Runnable() {
@Override
public void run() {
boolean downloading = true;
while (downloading) {
DownloadManager.Query q = new DownloadManager.Query();
q.setFilterById(downloadId);
Cursor cursor = manager.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);
runOnUiThread(new Runnable() {
@Override
public void run() {
mProgressBar.setProgress((int) dl_progress);
}
});
cursor.close();
}
}
}).start();