0
private class DownloadTextTask extends AsyncTask<String,Long,Long> { 

     CharSequence contentText; 
     Context context; 
     CharSequence contentTitle; 
     PendingIntent contentIntent; 
     int ID = 1; 
     long time; 
     int icon; 
     CharSequence tickerText; 

     @Override 
     protected Long doInBackground(String... urls) { 
      InputStream inputStream = null; 
      try { 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpResponse httpResponse = httpclient.execute(new HttpGet(urls[0])); 
       inputStream = httpResponse.getEntity().getContent(); 
       byte[] buffer = IOUtils.toByteArray(inputStream); 
       FileOutputStream fos = new FileOutputStream(MEDIA_PATH + "/fileName.mp3"); 
       fos.write(buffer); 
       fos.flush(); 
       fos.close();  
      } catch (Exception e) { 
      } 
      return (long) 100; 
     } 

     @Override 
     protected void onPostExecute(Long result) { 
      contentText = result + "% complete"; 
      contentTitle="Downloading Finished!"; 
      notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 
      notificationManager.notify(ID, notification); 
     } 

     @Override 
     protected void onPreExecute() { 
       super.onPreExecute(); 
       downloadNotification(); 
     } 

     @Override 
     public void onProgressUpdate(Long... progress) { 
       super.onProgressUpdate(progress); 
       contentText = progress[0] + "% complete"; 
       notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 
       notificationManager.notify(ID, notification); 
     } 

      public void downloadNotification(){ 
       String ns = Context.NOTIFICATION_SERVICE; 
       notificationManager = (NotificationManager) getSystemService(ns); 

       icon = R.drawable.downicon; 
       //the text that appears first on the status bar 
       tickerText = "Downloading..."; 
       time = System.currentTimeMillis(); 

       notification = new Notification(icon, tickerText, time); 

       context = getApplicationContext(); 
       //the bold font 
       contentTitle = "Your download is in progress"; 
       //the text that needs to change 
       contentText = "0% complete"; 
       Intent notificationIntent = new Intent(Intent.ACTION_VIEW); 
       // notificationIntent.setType("audio/*"); 
       contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT); 

       notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 
       notificationManager.notify(ID, notification); 

      } 
    } 

我已經寫了這段代碼來下載一個mp3文件,這裏的問題在於,它沒有更新下載文件的進度!我使用IOUtils類將InputStream轉換爲byte []。在這種情況下,我不知道如何發表進展!請幫助我。onProgressUpdate無法正常工作

+0

請參閱AsyncTask Java文檔上的示例。它顯示瞭如何正確使用'publishProgress''方法; http://developer.android.com/reference/android/os/AsyncTask.html – harism 2013-04-10 19:19:57

回答

0

我不認爲這是對IOUtils.toByteArray(..)的方式來提供的最新進展。如果文件很大,您可能不希望將整個內容讀入內存。您可以使用CountingInputStream來跟蹤讀取的總字節數。

public long countContent(URL urls) { 
    try { 
    //... 
    CountingInputStream counter = new CountingInputStream(httpResponse.getEntity().getContent()); 
    FileOutputStream os = new FileOutputStream(MEDIA_PATH + "/fileName.mp3"); 

    int read; 
    byte[] buffer = new byte[1028]; 
    while ((read = counter.read(buffer)) != -1) { 
     os.write(buffer, 0, read); 
     publishProgress(counter.getByteCount()/size); 
    } 
    // ... 
    return counter.getByteCount()/size; 
    } catch (IOException ex) { 
    throw new RuntimeException(ex); 
    } 
} 
+0

這是什麼尺寸? – Desire 2013-04-10 19:50:34

+0

從響應中獲取大小... – Frohnzie 2013-04-10 20:19:23

+0

非常感謝好友! – Desire 2013-04-10 20:48:08

1

你需要調用publishProgress(PARAM)在doInBackground()

http://developer.android.com/reference/android/os/AsyncTask.html

  1. doInBackground(參數...),onPreExecute後,立即在後臺線程()調用執行完畢 。此步驟用於執行可能需要很長時間的後臺計算。異步任務的參數傳遞給此步驟。計算結果必須通過該步驟返回並返回到最後一步。 此步驟還可以使用publishProgress(Progress ...)發佈一個或多個進度單元。這些值在onProgressUpdate(Progress ...)步驟中發佈在UI線程上。

  2. onProgressUpdate(Progress ...),在調用publishProgress(Progress ...)後在UI線程上調用。執行的時間是未定義的。 此方法用於在用戶界面顯示任何形式的進度,而後臺計算仍在執行。例如,它可以用來爲進度條設置動畫效果或在文本字段中顯示日誌。

一個例子@http://www.androidhive.info/2012/04/android-downloading-file-by-showing-progress-bar/

 OutputStream output = new FileOutputStream("/sdcard/file_name.extension") 
     long total = 0; 
     int count; 
     while ((count = inputStream.read(buffer) != -1) { 
      total += count; 
      // publishing the progress.... 
      publishProgress((int) (total * 100/fileLength)); 
      output.write(buffer, 0, count); 
     } 
+0

我使用IOUtils類將InputStream轉換爲byte []。在這種情況下,我不知道如何發表進展! – Desire 2013-04-10 19:21:13

+0

檢查答案中的最後一個鏈接。 – Raghunandan 2013-04-10 19:26:49

+0

我不知道字節[]大小! – Desire 2013-04-10 19:38:25

0

AsyncTask的所有功能都訪問符protected

所以它應該是:

@Override 
    protected void onProgressUpdate(Long... progress) { 
      super.onProgressUpdate(progress); 
      contentText = progress[0] + "% complete"; 
      notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 
      notificationManager.notify(ID, notification); 
    } 
+0

this方法不工作! – Desire 2013-04-10 19:39:02

+0

嘗試評論'super.onProgressUpdate(progress);' – 2013-04-10 19:39:47