[英]Android Download Listner
Is there any way to find out that the download is successfully or not. 有什么方法可以确定下载成功与否。 i am using following code to download files from other mobiles phones p2p, but some time other user is out of wifi range. 我正在使用以下代码从其他手机p2p下载文件,但有时其他用户不在wifi范围内。 the i wanted to know that the source is out of range so download is not possible at that time. 我想知道来源超出范围,因此当时无法下载。
is there any listener type thing that can help me ? 有没有可以帮助我的听众类型的东西?
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(aryToReturn.get(position).name)
.setDescription("File Downloding...")
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,aryToReturn.get(position).name));
I have done this using following code 我使用以下代码完成了此操作
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);
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.