如何显示下载进度?

How to Show Downloading Progress?

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

谁能帮帮我。 这是我的代码:-

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% 剩余...明智 **

简单替换

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

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

前者是从0增长到100,后者是从100增长到0

只需添加此代码:

     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]));
        }