我正在使用AsyncTask从webview下载不同的mp3。当我跟踪下载进度并传递给onprogress更新时,无论我做什么,它都会收到0。我尝试过使用双打,浮点数和整数。这是我的代码示例:
public class DownloadFile extends AsyncTask<Void, Float, String> {
ProgressBar progressBar;
NotificationManager notificationManager;
Notification notification2;
@Override
protected void onPreExecute() {
// configure the intent
Intent intent = new Intent();
final PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
// configure the notification
notification2 = new Notification(R.drawable.download, "DOWNLOADING: " + filename3, System
.currentTimeMillis());
notification2.flags = notification2.flags | Notification.FLAG_ONGOING_EVENT;
notification2.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.download_progress);
notification2.contentIntent = pendingIntent;
notification2.contentView.setTextViewText(R.id.percentage,"hi" );
notification2.contentView.setTextViewText(R.id.status_text, "DOWNLOADING: " + filename3);
notification2.contentView.setProgressBar(R.id.status_progress, 100, 0, false);
getApplicationContext();
notificationManager = (NotificationManager) getApplicationContext().getSystemService(
Context.NOTIFICATION_SERVICE);
notificationManager.notify(2, notification2);
}
@Override
protected String doInBackground(Void... params) {
try {
//set the download URL, a url that points to a file on the internet
//this is the file to be downloaded
URL url = songURL2;
//create the new connection
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//set up some things on the connection
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
//and connect!
urlConnection.connect();
//set the path where we want to save the file
//in this case, going to save it on the root directory of the
//sd card.
SDCardRoot = Environment.getExternalStorageDirectory() + "/download/";
//create a new file, specifying the path, and the filename
//which we want to save the file as.
File file = new File(SDCardRoot,filename3);
//this will be used to write the downloaded data into the file we created
FileOutputStream fileOutput = new FileOutputStream(file);
//this will be used in reading the data from the internet
InputStream inputStream = urlConnection.getInputStream();
//this is the total size of the file
Integer totalSize = urlConnection.getContentLength();
//variable to store total downloaded bytes
float downloadedSize = 0;
//create a buffer...
byte[] buffer = new byte[1024];
int bufferLength = 0; //used to store a temporary size of the buffer
//now, read through the input buffer and write the contents to the file
while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
//add the data in the buffer to the file in the file output stream (the file on the sd card
fileOutput.write(buffer, 0, bufferLength);
//add up the size so we know how much is downloaded
downloadedSize += bufferLength;
//this is where you would do something to report the prgress, like this maybe
publishProgress((downloadedSize * 100)/totalSize) * 100);
}
//close the output stream when done
fileOutput.close();
return "Success";
//catch some possible errors...
} catch (MalformedURLException e) {
e.printStackTrace();
return "Failed";
} catch (IOException e) {
e.printStackTrace();
return "Failed";
}
}
protected void onProgressUpdate(Float... progress) {
notification2.contentView.setProgressBar(R.id.status_progress, 100, (int)progress[0], false);
String stringy = progress + "%";
TextView textytext = (TextView) findViewById(R.id.percentage);
textytext.setText(stringy);
notificationManager.notify(2, notification2);
}
@Override
protected void onPostExecute(String result) {
notificationManager.cancel(2);
}
}
昨天和整个晚上我整天都在努力。 super.onprogressupdate()与此案例有什么关系,有谁知道解决方案?我把头发拉了出来。
编辑:哦,我正在尝试使用它,因此我可以将此进度发布到我在自定义通知中的进度条,但您可以通过代码告诉它。编辑#2:所以我一直在做一些测试,我把进度改为正确的算法
myProgress =(downloadedSize / totalSize)* 100;
然后我说如果myProgress&gt; 50然后发表进步(50);通过执行此操作,当下载完成一半时,进度条跳转到50。 然后我放弃我的变量,它不会将变量数据传递给onProgressUpdated。
有谁知道为什么硬编码的数字有效但不是变量中的数字?
答案 0 :(得分:0)
记录,记录,记录!记录下这种情况,看看你的输入,输出和中间值在任何特定时刻都在发挥作用。 (或者在调试器中使用断点,但这是另一个更复杂的故事)。
你确定getContentLength()正在返回你认为它返回的内容吗?此外,您将完成/总值乘以100两次,所以也请检查您的数学。尝试插入一些日志记录语句,并确保实际上发送您认为要发送给publishProgress的值。 (例如,尝试只将publishProgress(94.0)
放在一个百灵鸟上。我怀疑这个问题与内容长度没有被服务器报告或你的算法关闭 - 或者可能是数据足够小以至于你实际上没有看到中间值(大部分时间在wifi /上) 3G用于建立连接并等待延迟;实际的数据传输部分通常非常快,特别是对于较小的文件)。
答案 1 :(得分:0)
对我来说听起来像java问题,我自己是java新手。我最近在java中发现,当划分整数类型时,无论如何都会得到一个int值。尝试将totalSize声明为float或(float)在进行除法时将其转换为float
我也注意到了
publishProgress((downloadedSize * 100)/totalSize) * 100);
看起来很奇怪,也许你试过一些黑客来使代码工作,不应该是downloadSize / totalSize * 100吗?
答案 2 :(得分:0)
所以我一直在做很多测试,我已经让它与Android模拟器完美配合,我所做的就是说
if (myProgress is > 1) && (myProgress > previousProgress) {
previousProgress = myProgress
publishProgress(myProgress);}
奇怪的是,在模拟器中它每次都很完美,但使用我的手机进度条不会进展。起初它做了一两次现在它不适用于任何歌曲。而模仿者为任何歌曲进步。为什么这会在模拟器上运行而不是真正的手机?
答案 3 :(得分:0)
使用publishProgress((downloadedSize * 100) /totalSize) );
每次获得零的原因是因为将downloadedSize除以totalSize将结果舍入为零,因此你的结果总是为零。