2011-06-02 75 views
5

我試圖使用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); 
     } 
    } 
+0

也許你經常調用publishProgress()?如何將'(downloadedSize/totalSize)* 100'的最後一個值保存爲一個整數,並且只有當它與之前的值不同時才調用'publishProgress()'? – dmon 2011-06-02 02:22:10

+0

偉大的想法,除了現在它不凍結,但給我以下錯誤(我已經編輯它的主要帖子) – jsw 2011-06-02 03:36:16

+0

你肯定需要包括其餘的代碼。我們無法判斷爲什麼什麼是空的,爲什麼沒有它。所有你需要做的是解決這個問題,仔細跟蹤堆棧跟蹤並記錄下發生了什麼。 – 2011-06-02 03:56:44

回答

7

使用mod運算符僅更新5,10或20%。您在下載過程中不斷調用onProgressUpdate()。

if (downloadedSize % (totalSize/20) == 0) { // 20 updates every 5%, 10 every 10% and 5 every 20%, etc. 
    publishProgress((downloadedSize/totalSize) * 100); 
} 
+1

乾杯!雖然你的代碼實際上並不適用於我的情況,但我將它改編成了我的'publishProgress ()'檢查自上次通知更新後的'downloadedSize'是否大於'totalSize'的5%。現在很好用,歡呼! – jsw 2011-06-02 04:39:45

+1

您指向正確的方向!非常感謝! – 2016-03-06 21:31:29