我已经使用以下代码完成了此操作
private class downloadTask extends AsyncTask<String, Void, String> {
ProgressDialog dialog = new ProgressDialog(webServices.this);
@Override
protected String doInBackground(String... params) {
//Uri uri=Uri.parse("http://commonsware.com/misc/test.mp4");
Uri uri=Uri.parse(downLoadLink);
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
.mkdirs();
lastDownload=
mgr.enqueue(new DownloadManager.Request(uri)
.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |
DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setTitle(downloadFileName)
.setDescription("File Downloding...")
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
downloadFileName));
// Log.d("-->", String.valueOf(lastDownload));
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "Executed";
}
@Override
protected void onPostExecute(String result) {
dialog.dismiss();
Cursor c=mgr.query(new DownloadManager.Query().setFilterById(lastDownload));
if (c==null) {
//Toast.makeText(this, "Download not found!", Toast.LENGTH_LONG).show();
}
else {
c.moveToFirst();
Log.e("yes", statusMessage(c));
// Toast.makeText(this, statusMessage(c), Toast.LENGTH_LONG).show();
}
}
@Override
protected void onPreExecute() {
dialog.setMessage("Please wait...");
dialog.setCancelable(false);
dialog.show();
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
private String statusMessage(Cursor c) {
String msg="???";
switch(c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS))) {
case DownloadManager.STATUS_FAILED:
msg="Download failed!";
break;
case DownloadManager.STATUS_PAUSED:
msg="Download paused!";
break;
case DownloadManager.STATUS_PENDING:
msg="Download pending!";
break;
case DownloadManager.STATUS_RUNNING:
msg="Download in progress!";
break;
case DownloadManager.STATUS_SUCCESSFUL:
msg="Download complete!";
break;
default:
msg="Download is nowhere in sight";
break;
}
return(msg);
}