Android 显示进度为百分比的对话框
我正在使用异步任务从internet下载一些文件。对于这件事,我知道向用户展示到目前为止的进展是非常相关的。我下载文件的所有实现都非常成功,但唯一的问题是“进度”对话框,即使下载命令已启动,也显示为0% 我就是这样做的Android 显示进度为百分比的对话框,android,Android,我正在使用异步任务从internet下载一些文件。对于这件事,我知道向用户展示到目前为止的进展是非常相关的。我下载文件的所有实现都非常成功,但唯一的问题是“进度”对话框,即使下载命令已启动,也显示为0% 我就是这样做的 // Show Dialog Box with Progress bar @Override protected Dialog onCreateDialog(int id) { switch (id) { case progress_bar_ty
// Show Dialog Box with Progress bar
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case progress_bar_type:
prgDialog = new ProgressDialog(this);
prgDialog.setMessage("Downloading Mp3 file. Please wait...");
prgDialog.setIndeterminate(false);
prgDialog.setMax(100);
prgDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
prgDialog.setCancelable(false);
prgDialog.show();
return prgDialog;
default:
return null;
}
}
// Async Task Class
class DownloadMusicfromInternet extends AsyncTask<String, String, String> {
// Show Progress bar before downloading Music
@Override
protected void onPreExecute() {
super.onPreExecute();
// Shows Progress Bar Dialog and then call doInBackground method
showDialog(progress_bar_type);
}
// Download Music File from Internet
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
// Get Music file length
int lenghtOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(),10*1024);
// Output stream to write file in SD card
OutputStream output = new FileOutputStream(f_url[1]);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// Publish the progress which triggers onProgressUpdate method
publishProgress("" + (int) ((total * 100) / lenghtOfFile));
// Write data to file
output.write(data, 0, count);
}
// Flush output
output.flush();
// Close streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
// While Downloading Music File
protected void onProgressUpdate(String... progress) {
// Set progress percentage
prgDialog.setProgress(Integer.parseInt(progress[0]));
}
// Once Music File is downloaded
@Override
protected void onPostExecute(String file_url) {
// Dismiss the dialog after the Music file was downloaded
dismissDialog(progress_bar_type);
Toast.makeText(getApplicationContext(), "Download complete, playing Music", Toast.LENGTH_LONG).show();
}
}
//显示带有进度条的对话框
@凌驾
受保护的对话框onCreateDialog(int id){
开关(id){
案例进度条类型:
prgDialog=新建进度对话框(此对话框);
设置消息(“正在下载Mp3文件,请稍候…”);
prgDialog.setUndeterminate(假);
prgDialog.setMax(100);
prgDialog.setProgressStyle(ProgressDialog.STYLE_水平);
prgDialog.setCancelable(假);
prgDialog.show();
返回prgDialog;
违约:
返回null;
}
}
//异步任务类
类从Internet下载音乐任务{
//下载音乐前显示进度条
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//显示进度条对话框,然后调用doInBackground方法
显示对话框(进度条类型);
}
//从Internet下载音乐文件
@凌驾
受保护的字符串doInBackground(字符串…f_url){
整数计数;
试一试{
URL=新URL(f_URL[0]);
URLConnection conconnection=url.openConnection();
conconnect.connect();
//获取音乐文件长度
int lenghtOfFile=conconnect.getContentLength();
//读取文件的输入流-带8k缓冲区
InputStream输入=新的BufferedInputStream(url.openStream(),10*1024);
//将文件写入SD卡的输出流
OutputStream output=新文件OutputStream(f_url[1]);
字节数据[]=新字节[1024];
长总计=0;
而((计数=输入。读取(数据))!=-1){
总数+=计数;
//发布触发onProgressUpdate方法的进度
出版进度(“+(int)((总计*100)/长度文档));
//将数据写入文件
输出.写入(数据,0,计数);
}
//冲洗输出
output.flush();
//合流
output.close();
input.close();
}捕获(例外e){
Log.e(“错误:,e.getMessage());
}
返回null;
}
//下载音乐文件时
受保护的void onProgressUpdate(字符串…进度){
//设置进度百分比
prgDialog.setProgress(Integer.parseInt(progress[0]));
}
//下载音乐文件后
@凌驾
受保护的void onPostExecute(字符串文件\u url){
//下载音乐文件后关闭对话框
解雇对话框(进度条类型);
Toast.makeText(getApplicationContext(),“下载完成,播放音乐”,Toast.LENGTH_LONG.show();
}
}
请告诉我如何根据下载时间移动进度。谢谢你的帮助
更新
我认为这个问题与提到的重复问题不同,我研究后的问题来自int lenghtOfFile=conction.getContentLength()代码>
进度没有移动,因为getContentLength()
始终返回-1,因为它无法从服务器获取文件大小。我看到很多这样的问题没有得到回答。请问有出路吗?我很想知道。正如您在评论中所说,getContentLength()返回-1。这会导致进度更新始终为负数,这意味着进度条永远不会前进。这就是为什么它没有像你想象的那样移动
根据javadoc,当没有内容长度头或者由于某种原因无法解析为数字时,返回-1。在这种情况下,您不能根据下载文件的大小提供进度表。正如您在评论中所说,getContentLength()返回-1。这会导致进度更新始终为负数,这意味着进度条永远不会前进。这就是为什么它没有像你想象的那样移动
根据javadoc,当没有内容长度头或者由于某种原因无法解析为数字时,返回-1。在这种情况下,无法根据下载文件的大小提供进度表。如果进度条设置为不确定,则无需调用prgDialog.setProgress(Integer.parseInt(progress[0])代码>。这可能就是我们没有取得进展的原因
下载开始时调用prgDialog.setIndeterminate(true)
,下载完成后调用prgDialog.setIndeterminate(false)
。使用不确定进度条时,避免调用setProgress
编辑:更正:如果条形图处于不确定模式,则调用setProgress
不会执行任何操作
另外,您说您正在将Undeterminate设置为true,但在onCreateDialog
方法中,我看到了prgDialog.setUndeterminate(false)
如果进度条设置为不确定,则无需调用prgDialog.setProgress(Integer.parseInt(progress[0])代码>。这可能就是我们没有取得进展的原因
下载开始时调用prgDialog.setIndeterminate(true)
,下载完成后调用prgDialog.setIndeterminate(false)
。使用不确定进度条时,避免调用setProgress
编辑:更正:如果条形图处于不确定模式,则调用setProgress
不会执行任何操作