我正在尝试使用AsyncTask下载文件,并通过通知进度条显示下载进度。一切都很顺利(在后台线程中下载),但每当我添加代码通过publishProgress()更新进度条时,它会冻结整个手机,直到下载完成并显示“下载完成”通知为止。
我完全不知道为什么会这样,但我认为可能是我使用的publishProgress((downloadedSize / totalSize) * 100)导致的?
无论如何,以下是DownloadDataTask:
我完全不知道为什么会这样,但我认为可能是我使用的publishProgress((downloadedSize / totalSize) * 100)导致的?
无论如何,以下是DownloadDataTask:
protected String doInBackground(RomDataSet... params) {
try {
// Download file here, all good
//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 / totalSize) * 100);
}
//close the output stream when done
fileOutput.close();
//catch some possible errors...
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPreExecute() {
Intent intent = new Intent(ListActivity.this, ListActivity.class);
pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
// configure the notification
notification = new Notification(R.drawable.ic_stat_rom, "Downloading Rom via RomGet", System
.currentTimeMillis());
notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
notification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.layout_download);
notification.contentIntent = pendingIntent;
notification.contentView.setImageViewResource(R.id.status_icon, R.drawable.icon_rom);
notification.contentView.setTextViewText(R.id.download_description, "Downloading");
notification.contentView.setProgressBar(R.id.download_progress, 100, 0, false);
getApplicationContext();
notificationManager = (NotificationManager) getApplicationContext().getSystemService(
Context.NOTIFICATION_SERVICE);
notificationManager.notify(43, notification);
}
@Override
protected void onProgressUpdate(Integer... progress) {
//notification.contentView.setProgressBar(R.id.download_progress, 100, Math.round(progress[0]), false);
notification.contentView.setTextViewText(R.id.download_description, Integer.toString(progress[0]));
// inform the progress bar of updates in progress
notificationManager.notify(43, notification);
}
@Override
protected void onPostExecute(String result) {
notification.contentView.setProgressBar(R.id.download_progress, 100, 100, false);
notification.contentView.setTextViewText(R.id.download_description, "Done");
notificationManager.notify(43, notification);
}
我真的被这个问题卡住了 - 任何帮助都将不胜感激。谢谢。
@Override
protected void onProgressUpdate(Integer... progress) {
if ((progress[0] - lastSize) > 5000) {
lastSize = progress[0];
notification.contentView.setProgressBar(R.id.download_progress, totalSize, progress[0], false);
//notification.contentView.setTextViewText(R.id.download_description, Integer.toString(progress[0]));
// inform the progress bar of updates in progress
notificationManager.notify(43, notification);
}
}
publishProgress()
的频率太高了?把(downloadedSize / totalSize) * 100
的最后一个值保存在一个整数中,只有当它与上一个值不同时才调用publishProgress()
,如何? - dmon