【发布时间】:2017-03-08 17:35:39
【问题描述】:
在我的应用程序中,当我单击文件时,它将开始从服务器下载,进度将显示在手机状态栏中。但我希望进度条显示在我的应用程序屏幕上。
我点击了这个链接:-
http://www.androidhive.info/2012/04/android-downloading-file-by-showing-progress-bar/
在处理问题陈述时,我遇到了这些SYSTEM_DOWNLOADER 和APP_DOWNLOADER。
这些到底是做什么的?
上面的例子是在activity中调用的。我想知道如何在片段中调用带有下载进度条的异步任务。
我正在编辑问题以进行澄清,请帮助
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
MoodleModule module = listObjects.get(position).module;
if (module == null)
return;
Intent i = new Intent(context, AppBrowserActivity.class);
String modurl = module.getUrl();
String courseurl = session.getmUrl()
+ "/course/view.php?id=" + courseid;
modurl = (modurl == null) ? courseurl : modurl;
i.putExtra("url", modurl);
if (!module.getModname().contentEquals("resource")) {
context.startActivity(i);
return;
}
if (module.getContents() == null) {
context.startActivity(i);
return;
}
if (module.getContents().isEmpty()) {
context.startActivity(i);
return;
}
MoodleModuleContent content = module.getContents().get(0);
String path = "/s" + session.getCurrentSiteId() + "c"
+ courseid + "/";
File file = new File(Environment
.getExternalStoragePublicDirectory("/MDroid")
+ path + content.getFilename());
//TODO here
// Download if file doesn't already exist
if (!file.exists()) {
String fileurl = content.getFileurl();
fileurl += "&token=" + session.getToken();
fName=content.getFilename();
//FROM HERE I AM CALLING ASYNCHRONOUS TASK
startDownload();
} else {
FileOpener.open(context, file);
}
}
});
return convertView;
}
private void startDownload() {
String url = "http://farm1.static.flickr.com/114/298125983_0e4bf66782_b.jpg";
new DownloadFileAsync().execute(url);
}
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DOWNLOAD_PROGRESS:
mProgressDialog = new ProgressDialog(getActivity());
mProgressDialog.setMessage("Downloading file..");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(false);
return mProgressDialog;
default:
return null;
}
}
class DownloadFileAsync extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
/*mProgressDialog = new ProgressDialog(getActivity());
mProgressDialog.setMessage("Downloading file.");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(false);
mProgressDialog.show();*/
/*showDialog(DIALOG_DOWNLOAD_PROGRESS);*/
mProgressDialog.show();
}
@Override
protected String doInBackground(String... aurl) {
int count;
/*String fName=content.getFilename();*/
/*InputStream input = null;
OutputStream output = null;*/
HttpURLConnection connection = null;
try {
URL url = new URL(aurl[0]);
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
/* File f = new File(Environment.getExternalStorageDirectory()
+ "/sdcard/");
f.mkdirs();*/
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/some_photo_from_gdansk_poland.jpg");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {}
return null;
}
protected void onProgressUpdate(String... progress) {
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
/* mProgressDialog.dismiss();*/
}
@Override
protected void onPostExecute(String unused) {
/*dismissDialog(DIALOG_DOWNLOAD_PROGRESS);*/
mProgressDialog.dismiss();
}
}
当我点击文件时,应用程序强制停止。我在做什么错误?请帮助我。谢谢
【问题讨论】:
-
您可以使用
ProgressBar或ProgressDialog直到您的文件正在下载。 -
所以我需要知道这两件事的区别
-
如果您使用DownloadManager,那么您的进度将显示在通知栏中,直到您的文件下载,如果您在
AsyncTask中使用ProgressBar或ProgressDialog,那么它将在AsyncTask的onPostExecuted()方法中被解雇 -
我已经编辑了我的问题陈述。检查它并告诉我代码出了什么问题!
标签: android android-download-manager