0

I'm using java thread to download a zip file from an API.when i start download a file,the download progress will be displayed in the notification bar.when i scroll down the notification bar,it have no response.it just hang the device till the download complete.if download is done, then no problem in scroll notification bar.

my question is :

1.how to make the app run smoothly without any lag?is there any other way to handle download progress in android.

2.the app take some time to start download.is there any problem with the code that i am using.

here is my code :


 protected void doDownload(final String urlLink, final String fileName) {


        final Thread dx = new Thread() {

            public void run() {
                File root = android.os.Environment.getExternalStorageDirectory();
                File dir = new File(root.getAbsolutePath() + "/EriReader/temp/");
                if (dir.exists() == false) {
                    dir.mkdirs();

                }
                //Save the path as a string value
                try {
                    URL url = new URL(urlLink);
                    Log.i("FILE_NAME", "File name is " + fileName);
                    Log.i("FILE_URLLINK", "File URL is " + url);
                    URLConnection connection = url.openConnection();
                    connection.connect();
                    // this will be useful so that you can show a typical 0-100% progress bar
                    final int fileLength = connection.getContentLength();

                    // download the file
                    InputStream input = new BufferedInputStream(url.openStream());
                    OutputStream output = new FileOutputStream(dir + "/" + fileName);

                    byte data[] = new byte[1024];
                    long total = 0;
                    int count;
                    while ((count = input.read(data)) != -1) {
                        total += count;
                        if (fileLength > 0) {
                             int status = ((int) (total * 100 / fileLength));

                       //    Notification in notification bar
                            mBuilder.setProgress(100, status, false);
                            mNotifyManager.notify(id, mBuilder.build());
                        }
                        output.write(data, 0, count);
                    }

                    output.flush();
                    output.close();
                    input.close();

                } catch (Exception e) {
                    e.printStackTrace();
                    Log.i("DOWNLOADING err", "ERROR IS" + e);
                }


                mBuilder.setContentText("Download complete")
                        // Removes the progress bar
                        .setProgress(0, 0, false);
                mNotifyManager.notify(id, mBuilder.build());

            }

        };
}
Muhammed Riyas A V
  • 71
  • 1
  • 1
  • 11

1 Answers1

0

You can follow this link:you have to use AsyncTask for the UI updation in on Post Method

Amit Ranjan
  • 567
  • 2
  • 11