2012-07-23 129 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