java - onProgressUpdate 不起作用

标签 java android android-asynctask apache-commons-httpclient

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[]。我不知道在这种情况下如何发布进展!请帮助我。

最佳答案

需要在doInBackground()中调用publishProgress(param)

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

  1. doInBackground(Params...),在 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);
        }

关于java - onProgressUpdate 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15934487/

相关文章:

java - 给定由 LinkedList 组成的 java hashmap 中的键,如何更新值?

java - android 调用公共(public)方法并获得成功反馈

java - Android 中的无限按钮动画

android - 进度对话框在异步任务中只显示很短的时间并且 Activity 加载缓慢

android - AsyncTask超时情况解决方案

android - 是否存在使用 AsyncTask 优于 AsyncTaskLoader 的场景?

java - 如何使用栈来解析字符串

Java流图修改未内置类的自定义类对象

java - 在 Titan Graph 中查找连接组件的有效方法是什么

android - 谷歌地图 Android API : SupportMapFragment vs MapFragment