1

Can some one explain me what am i doing wrong, I'm downloading a file and I have a notification progress bar that get updated. The progress bars is working "fine" because it get updated, but the problem is that when the function is done downloading the fine my progress bar don't gets updated with the message that say Complete and I try to hide the progress bar by doing this...

 notification.setProgress(0, 0, false);
notification.setContentTitle("Complete");
notification.setOngoing(false);
notification.setPriority(NotificationCompat.PRIORITY_HIGH);
notificationManager.notify(idd, notification.build());
Log.e(TAG, "1");

my programs ignores all of that and only log 1

private boolean saveFile(ResponseBody body, String fileName, String id) {

        try {
            // todo change the file location/name according to your needs
            File futureStudioIconFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), fileName);

            InputStream inputStream = null;
            OutputStream outputStream = null;


            final int idd = Integer.parseInt(id);

            Intent activityIntent = new Intent(this, MainActivity.class);
            PendingIntent contentIntent = PendingIntent.getActivity(this, 0, activityIntent, 0);
            final NotificationCompat.Builder notification = new NotificationCompat.Builder(this, CHANNEL_2_ID)
                    .setSmallIcon(R.drawable.ic_cloud_download_black_24dp)
                    .setContentTitle("Download")
                    .setContentText("Downloading in process")
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setContentIntent(contentIntent);

            notificationManager.notify(idd, notification.build());

            try {
                byte[] fileReader = new byte[4096];
                long fileSize = body.contentLength();
                long fileSizeDownloaded = 0;

                inputStream = body.byteStream();
                outputStream = new FileOutputStream(futureStudioIconFile);

                while (true) {
                    int read = inputStream.read(fileReader);

                    if (read == -1) {

                        break;
                    }

                    outputStream.write(fileReader, 0, read);

                    fileSizeDownloaded += read;

                    Log.e(TAG, "file download: " + fileSizeDownloaded + " of " + fileSize);
                    Long fileSizeD = fileSizeDownloaded;
                    Long fileS     = fileSize;
                    int down = fileSizeD.intValue();
                    int max  = fileS.intValue();
                    Log.e(TAG, "MAX: " + max + " Down: "+ down);
                        notification.setProgress(max, down, false);
                        notificationManager.notify(idd, notification.build());
                }

                outputStream.flush();

                notification.setProgress(0, 0, false);
                notification.setContentTitle("Downloasdfasad");
                notification.setOngoing(false);
                notification.setPriority(NotificationCompat.PRIORITY_HIGH);
                notificationManager.notify(idd, notification.build());
                Log.e(TAG, "1");

                return true;
            } catch (IOException e) {
                return false;
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }

                if (outputStream != null) {
                    outputStream.close();

                }
            }
        } catch (IOException e) {
            return false;
        }

    }
Pedro
  • 1,440
  • 2
  • 15
  • 36

2 Answers2

2

Try like this

Intent activityIntent = new Intent(this, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, activityIntent, 0);
        final NotificationCompat.Builder notification = new NotificationCompat.Builder(this, CHANNEL_2_ID)
                .setSmallIcon(R.drawable.ic_cloud_download_black_24dp)
                .setContentTitle("Download")
                .setOnlyAlertOnce(true)
                .setContentText("Downloading in process")
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setContentIntent(contentIntent);

notificationManager.notify(idd, notification.build());

Then Again

notification.setProgress(0, 0, false);
notification.setContentTitle("Download Finished");
notificationManager.notify(idd, notification.build());
Log.e(TAG, "1");
return true;

Reference : https://stackoverflow.com/a/16435330/5502638

Akash Dubey
  • 1,508
  • 17
  • 34
1

You can try doing this inside the while loop

Im not sure if this is the correct way to do it, but it will solver your problem

int max2 = max - 62500;
                    if( max2 > down) {
                        notification.setProgress(max, down, false);
                        notificationManager.notify(idd, notification.build());
                    } else {
                            notification.setProgress(0, 0, false);
                            notification.setContentText("Download Complete");
                        notificationManager.notify(idd, notification.build());

                    }
Carlos Castro
  • 132
  • 1
  • 11