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