2013-06-27 4 views
8
私は現在、私のアプリの中にバックグラウンドで大きなファイルをダウンロードするには AsyncTaskを使用しています

は、現在、ダウンロードの進捗状況は以下の通りonProgressUpdateを介して更新されProgressDialogとして示されている:これは今しかし、I正常に動作ファイルをダウンロード中に通知を更新する際のUIの遅延を防ぐにはどうすればよいですか?

protected String doInBackground(String... sUrl) { 
     try { 
      String destName = sUrl[1]; 
      file_Delete(destName); // Just to make sure! 

      URL url = new URL(sUrl[0]); 
      URLConnection connection = url.openConnection(); 
      connection.connect(); 
      int fileLength = connection.getContentLength(); 

      InputStream input = new BufferedInputStream(url.openStream()); 
      OutputStream output = new FileOutputStream(destName); 

      byte data[] = new byte[1024]; 
      long total = 0; 
      int count; 
      while ((count = input.read(data)) != -1) { 
       total += count; 
       publishProgress((int) (total * 100/fileLength)); 
       output.write(data, 0, count); 
      } 

      output.flush(); 
      output.close(); 
      input.close(); 

     } catch (Exception e) { 
      Log.e(TAG, NAME + ": Error downloading file! " + e.getMessage()); 
      return e.getMessage(); 
     } 

     return null; 
    } 

@Override protected void onProgressUpdate(Integer... progress) { 
     super.onProgressUpdate(progress); 
     DownloadImage.mProgressDialog.setProgress(progress[0]); 


    } 

通知バーの通知を使用して、代わりにダウンロードを追跡したい(ファイルがかなり大きく、ユーザーはアプリの外から追跡したい)。

私はしかし、以下のコードを試してみましたUIがひどく遅れを開始し、私はたくさん呼ばなっpublishProgressにその原因を見ることができますので、どのように私は毎秒

@Override protected void onProgressUpdate(Integer... progress) { 
     super.onProgressUpdate(progress); 
     DownloadImage.mProgressDialog.setProgress(progress[0]); 

     DownloadImage.myNotification = new NotificationCompat.Builder(c) 
     .setContentTitle("Downloading SlapOS") 
     .setContentText("Download is " + progress[0] + "% done") 
     .setTicker("Downloading...") 
     .setOngoing(true) 
     .setWhen(System.currentTimeMillis()) 
     .setProgress(100, progress[0], false) 
     .setSmallIcon(R.drawable.icon) 
     .build(); 

     DownloadImage.notificationManager.notify(1, DownloadImage.myNotification); 

    } 
publishProgressを呼び出すための背景コードの変更については行くことができます
+0

あなたが現在実行中のAndroidのバージョンは何ですか? – JPM

+0

最小SDKのバージョンは8ですが、4.2.1を実行しているデバイスと2.3.4を実行しているデバイスの両方で同じ結果がテストされています(システムUIがクラッシュして再起動するまでUIが遅延します) –

答えて

14

はので、どのように私はpublishProgressを呼び出すためにバックグラウンド・コードを変更することについては行くことができる唯一の毎秒

私はで%を示したアップロード機能のために前にこれを行っていますしかし、同じ正確なアイデア。 AsyncTaskにはダウンロードがpercentDoneであり、のみと表示されます。が変更された場合はpublishProgressとなります。そうすれば、ダウンロードされた%が変更されたときにpublishProgressに電話をかけることになるので、Notificationを更新する必要があります。これはUIの遅れを解決するはずです。

私が提案した実装としてこれを書いていましたが、OPのように聞こえるようになっています。しかし、これは将来他の人に役立つかもしれません:

byte data[] = new byte[1024]; 
    long total = 0; 
    int count, latestPercentDone; 
    int percentDone = -1; 
    while ((count = input.read(data)) != -1) { 
     total += count; 
     latestPercentDone = (int) Math.round(total/fileLength * 100.0); 
     if (percentDone != latestPercentDone) { 
      percentDone = latestPercentDone; 
      publishProgress(percentDone); 
     } 
     output.write(data, 0, count); 
    } 
+0

完璧な遅延が解決されました! –

+0

これは私の必要性にも十分にいいですよ – brux

+0

私にはあまりにも遅れています! – VSG24

0

私は本当にあなたのアプローチが好きでした!コードを次のように変更すると、プログレスバーが正しく更新されることがわかりました。 math.round()を使用しているときにいくつか問題がありました。

latestPercentDone = (int) ((dataBytesWritten/(float) totalSize) * 100); 
    if (percentDone != latestPercentDone) { 
    percentDone = latestPercentDone; 
    publishProgress(percentDone); 
    } 
+0

これは、受け入れられた答えに何も追加しません。 – VSG24

関連する問題