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 ファイルをダウンロードするためにこのコードを書きましたが、ここでの問題は、ファイルのダウンロードの進行状況が更新されないことです! InputStream を byte[] に変換するために IOUtils クラスを使用しています。その場合の進捗状況を公開する方法がわかりません。親切に私を助けてください。

4

3 に答える 3

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);
     }
于 2013-04-10T19:31:17.407 に答える
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);
  }
}
于 2013-04-10T19:36:53.660 に答える