0

I'm trying to setup ProgressDialog to show to the user the progress of an InputStream of bytes. I'm able to show this in the console of the Android Studio by way of this below :

Log.d(TAG, "Progress, transferred " + Integer.toString(100 *  bytesTransferred/osSize) + "%");

However, I'm not sure how to attach the bytesTransferred/osSize correctly to a ProgressDialog, I can use a thread to time how long the progress bar will take to fill up the progressBar with the code below:

    update = new ProgressDialog(this);
    update.setTitle("Transferring update");
    update.setMessage("Please wait updating...");
    update.setCanceledOnTouchOutside(false);
    update.setProgress(0);

    final int totalProgressTime = 100;
    final Thread t = new Thread() {
        @Override
        public void run() {
            int jumpTime = 0;
            while(jumpTime < totalProgressTime) {
                try {
                    sleep(1800);
                    jumpTime += 1;
                    update.setProgress(jumpTime);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };
    t.start();

    update.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    update.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    update.show();
} 

But would be much better to show to the user the actual transfer of the bytes. This below is what I have to work with:

    final int osSize;
    final InputStream inputStream;

    osSize = inputStream();

    public void onProgress(int bytesTransferred) {
    Log.d(TAG, "Progress, transferred " + Integer.toString(100 * bytesTransferred/osSize) + "%");

    }

Any help would be appreciated, thanks in advance.

Jonas
  • 474
  • 3
  • 17
  • https://stackoverflow.com/questions/8379398/java-progressmonitorinputstream-using-existing-jprogressbar – hunter May 30 '17 at 07:59
  • I'm hoping to find a simply way to achieve this without have to write a different class in a complex way, but thanks for the link. I wonder if maybe just adding somehow to the onProgress which is already showing in the logcat the bytes being transferred, a ProgressDialog. I just need to know how to attach it together, if you understand what I trying to say. – Jonas May 30 '17 at 08:52

1 Answers1

0

Extend this:

    @Override
    public void run() {
        int jumpTime = 0;
        while(jumpTime < totalProgressTime) {
            try {
                sleep(1800);
                jumpTime += 1;
                update.setProgress(jumpTime);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

With this:

    @Override
    public void run() {
        int jumpTime = 0;
        while(jumpTime < totalProgressTime) {
            try {
                sleep(1800);
                jumpTime += 1;
                final int fJumpTime = jumpTime;
                runOnUiThread(new Runnable() {
                  @Override
                  public void run() {
                    update.setProgress(fJumpTime);
                });
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
Endre Börcsök
  • 477
  • 1
  • 8
  • 19
  • Thanks, However I get 'Variable 'JumpTime' is accessed from with inner class, needs to be declared final'. I then set to final and then the 'jumpTime +=1' then complains that you cannot assign a value to final variable 'JumpTime'. Any ideas on this ? – Jonas May 30 '17 at 10:34
  • True. Sorry, I didn't see that for the first time, check my edit :). – Endre Börcsök May 30 '17 at 15:27