【问题标题】:How to Show Downloading Progress?如何显示下载进度?
【发布时间】:2016-02-25 11:55:29
【问题描述】:

我正在尝试在媒体下载时显示进度。 歌曲已正确下载并存储现在我想显示下载了多少 百分比

所以任何人都可以帮助我。 这是我的代码:-

class DownloadFile extends AsyncTask<String, String, String> {
                    @Override
                    protected String doInBackground(String... url) {

               //............ declration and all
       if (fsize != lenghtOfFile) {
                        int filesize = f.getAbsolutePath().length();
                        if (filesize != lenghtOfFile) {
                            InputStream input = new BufferedInputStream(url1.openStream());
                            OutputStream output = new FileOutputStream(f);
                            byte data[] = new byte[1024];
                            long total = 0;
                            while ((count = input.read(data)) != -1) {
                                total += count;
                                publishProgress("" + (int) ((total * 100) / lenghtOfFile));
                              //  pDialog.setMessage( String.valueOf((int) ((total * 100) / lenghtOfFile)) +"Downloaded.");
                                output.write(data, 0, count);
                            }
                            output.flush();
                            output.close();
                            input.close();
                        }
                 //...... other code
                    }

                    @Override
                    protected void onProgressUpdate(String... values) {
                        super.onProgressUpdate(values);
                    }

                    @Override
                    protected void onPostExecute(String result) {
                    //on post over  
                      pDialog.dismiss();
                    }
                    @Override
                    protected void onPreExecute() {
                   //set Dialog               
                  pDialog = new ProgressDialog(getActivity());
                        pDialog.setMessage("Please wait song is Downloading...");
                        pDialog.setIndeterminate(false);
                        pDialog.setCancelable(false);
                        pDialog.show();
                    }
                }

**所有的东西都运行良好.... 只是 我想显示剩余 99%、98% 的百分比......同样明智 **

【问题讨论】:

    标签: java android android-asynctask progressdialog


    【解决方案1】:

    只需添加此代码:

         protected void onPreExecute() {
                    pDialog = new ProgressDialog(getActivity());
                    pDialog.setMessage("Downloading...");
                    pDialog.setIndeterminate(false);
                    pDialog.setMax(100);
                    pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                    pDialog.setCancelable(true);
                    pDialog.show();
                }
     @Override
            protected void onProgressUpdate(String... values) {
                super.onProgressUpdate(values);
                pDialog.setProgress(Integer.parseInt(values[0]));
            }
    

    【讨论】:

      【解决方案2】:

      简单替换

      publishProgress("" + (int) ((total * 100) / lenghtOfFile));
      

      publishProgress("" + (int) (((lenghtOfFile - total) * 100) / lenghtOfFile));
      

      前一个从0增长到100,而后一个从100增长到0。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-19
        • 1970-01-01
        • 2021-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-13
        相关资源
        最近更新 更多