0

我有一个文件下载应用程序,我需要使用 Asnc Task 方法更新进度 进度将由 Dialog 和 Notification Bar 同时显示,就像 Google PLay 中的示例一样

当我运行该方法时,通知栏显示非常缓慢的响应。在 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();

}
4

2 回答 2

1

您正在非常频繁地更新通知栏,因此响应缓慢。在 2-3 秒内更新一次,你会没事的。

于 2012-07-23T07:46:30.133 回答
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();

    }
于 2012-07-23T04:00:40.983 回答