我試圖使用AsyncTask
下載帶有通知進度的文件。一切工作正常(下載後臺線程),除非我通過publishProgress()
添加代碼來更新進度條,它會凍結整個手機,直到下載完成並且通知顯示「下載完成」。使用AsyncTask凍結UI線程
我完全失去了爲什麼這是這種情況,但我可能認爲它是沿着我使用的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 2011-06-02 02:22:10
偉大的想法,除了現在它不凍結,但給我以下錯誤(我已經編輯它的主要帖子) – jsw 2011-06-02 03:36:16
你肯定需要包括其餘的代碼。我們無法判斷爲什麼什麼是空的,爲什麼沒有它。所有你需要做的是解決這個問題,仔細跟蹤堆棧跟蹤並記錄下發生了什麼。 – 2011-06-02 03:56:44