2012-07-23 134 views
0

我有我需要使用ASNC工作方法 的進展將通過對話和通知欄顯示更新進度文件下載的應用程序同時,就像在谷歌的樣片播放異步TaskUI更新

當我運行該方法,Notification Bar顯示響應速度非常慢。 在ONProgressUpdate中,我只更新兩個UI進度而沒有同步化。

當我使用線程睡眠來減慢進程時,它運行平穩但下載速度非常低。

如何確保兩個UI線程平穩運行,同時保持可接受的快速下載速度?

這是代碼

請回答:)下面

@Override 
protected void onProgressUpdate(Integer... values) { 
    if (values.length>0) { 
     //Log.d("Download Activity", "progressUpdate:"+values[0]); 
     if (values[0] < 0) { 
      this.cancel(true); 
      showNetworkErrorDialog(); 
     } else { 
      //size += values[0]; 
      //Log.d("Download Activity", "download size:"+size); 
      //downloadedGoodListSize += values[0]; 
      //downloadProgressBar.incrementProgressBy(values[0]); 
      setCurrentProgress(values[0]); 
      downloadProgressBar.setProgress(values[0]); 
      double currentProg = downloadProgressBar.getProgress()*1.0/downloadProgressBar.getMax()*100; 
      //progressTextView.setText(decimalFormat.format(downloadedGoodListSize*1.0/downloadProgressBar.getMax()*100.0)+"%"); 
      progressTextView.setText(decimalFormat.format(downloadProgressBar.getProgress()*1.0/downloadProgressBar.getMax()*100)+"%"); 


      notification.contentView.setProgressBar(R.id.pb, 100, (int)currentProg, false); 

      nm.notify(notificationID, notification); 

     } 
    } 
} 

@Override 
protected void onPostExecute(Boolean result) { 
    Log.i("Download Activity", "onPostExecute: starting"); 
    downloadProgressBar.setProgress(downloadProgressBar.getMax()); 
    progressTextView.setText(100+"%"); 
    wheelProgressBar.setVisibility(View.GONE); 
    downloadingTextView.setText(getFinishedText()); 

    notification.contentView.setProgressBar(R.id.pb, 100, 100, false); 
    nm.notify(notificationID, notification); 
    nm.cancelAll(); 

    Log.i("Download Activity", "onPostExecute: set interface ok"); 
    finishDialog.show(); 

} 
+1

發表了一些代碼,很難提出解決方案,而沒有看到問題出在哪裏。 – dmon 2012-07-23 03:40:47

+0

你必須發佈你的代碼在問題頁面不在answere的刪除下面一個.... – 2012-07-23 04:09:48

+0

對不起,我會採取額外的通知。 – 2012-07-23 04:12:40

回答

0

是我的代碼

@Override 
    protected void onProgressUpdate(Integer... values) { 
     if (values.length>0) { 
      //Log.d("Download Activity", "progressUpdate:"+values[0]); 
      if (values[0] < 0) { 
       this.cancel(true); 
       showNetworkErrorDialog(); 
      } else { 
       //size += values[0]; 
       //Log.d("Download Activity", "download size:"+size); 
       //downloadedGoodListSize += values[0]; 
       //downloadProgressBar.incrementProgressBy(values[0]); 
       setCurrentProgress(values[0]); 
       downloadProgressBar.setProgress(values[0]); 
       double currentProg = downloadProgressBar.getProgress()*1.0/downloadProgressBar.getMax()*100; 
       //progressTextView.setText(decimalFormat.format(downloadedGoodListSize*1.0/downloadProgressBar.getMax()*100.0)+"%"); 
       progressTextView.setText(decimalFormat.format(downloadProgressBar.getProgress()*1.0/downloadProgressBar.getMax()*100)+"%"); 


       notification.contentView.setProgressBar(R.id.pb, 100, (int)currentProg, false); 

       nm.notify(notificationID, notification); 

      } 
     } 
    } 

@Override 
    protected void onPostExecute(Boolean result) { 
     Log.i("Download Activity", "onPostExecute: starting"); 
     downloadProgressBar.setProgress(downloadProgressBar.getMax()); 
     progressTextView.setText(100+"%"); 
     wheelProgressBar.setVisibility(View.GONE); 
     downloadingTextView.setText(getFinishedText()); 

     notification.contentView.setProgressBar(R.id.pb, 100, 100, false); 
     nm.notify(notificationID, notification); 
     nm.cancelAll(); 

     Log.i("Download Activity", "onPostExecute: set interface ok"); 
     finishDialog.show(); 

    } 
+0

不要將您的代碼作爲'Answer'發佈在您的原始問題中。你會看到一個鏈接'編輯'在你的問題發佈你的代碼他們的.. – 2012-07-23 04:10:30

1

要更新的通知欄,非常頻繁,因此響應速度慢。在2-3秒內更新一次,你會沒事的。

+0

它是減少更新頻率的進展是在doInBackground設置? – 2012-07-24 04:09:50

+0

是的,這使得通知欄'太忙'不能與 – 2012-07-24 04:52:45

+0

互動要解釋,是否適當設置日曆實例並限制時間間隔以減少更新任務的價值? – 2012-07-24 06:58:21