0

I use the following code to download files from the server on android

Code For Download Activity:

public class DownloadActivity extends AppCompatActivity {

    ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_download);

        progressDialog = new ProgressDialog(this);
        progressDialog.setIndeterminate(false);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setMax(100);
        progressDialog.setCancelable(false);
        progressDialog.show();

        String url = "https://www.google.com.sa/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png";
        String fileName = url.substring(url.lastIndexOf('/') + 1);

        Intent intent = new Intent(DownloadActivity.this, DownloadService.class);
        intent.putExtra("url", "https://www.google.com.sa/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png");
        intent.putExtra("filename", fileName);
        intent.putExtra("receiver", new DownloadReceiver(new Handler()));
        startService(intent);
    }

    private class DownloadReceiver extends ResultReceiver {
        public DownloadReceiver(Handler handler) {
            super(handler);
        }

        @Override
        protected void onReceiveResult(int resultCode, Bundle resultData) {
            super.onReceiveResult(resultCode, resultData);
            if (resultCode == DownloadService.UPDATE_PROGRESS) {
                int progress = resultData.getInt("progress");
                Log.d("PROGRESS", String.valueOf(progress)); // Here works well

                progressDialog.setProgress(progress); // Here is the problem
                if (progress == 100) {
                    progressDialog.dismiss();
                }
            }
        }
    }
}

The code work's as intended, but the progressDialog.setProgress(progress); doesn't show anything

The output displayed in logs by Log.d() is as expected.

I don't know what the problem is?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
A. El-zahaby
  • 1,130
  • 11
  • 32
  • Not sure what is ResultReceiver, but I think you should use the handler in order to update the progress dialog, only the uiThread can change/modify the UI elements. Try to use hanlder.post and put the setProgress inside – Anton Makov Mar 31 '18 at 16:43
  • Possible duplicate of [ProgressDialog setProgress and setMessage inside asyncTask don't work](https://stackoverflow.com/questions/28983419/progressdialog-setprogress-and-setmessage-inside-asynctask-dont-work) – Santanu Sur Mar 31 '18 at 16:45
  • @SantanuSur I've added an update on the question ... I added `setMax ()` and `setIndeterminate (false)` and did not work – A. El-zahaby Mar 31 '18 at 17:00
  • read the answer that i have posted !! (link) – Santanu Sur Mar 31 '18 at 17:21

2 Answers2

1

As I mentioned in my first comment try to use the handler the constructor of your worker thread(I assume) is getting. You have access for it because you wrote super(handler).

put this code where do you wish to update your progress dialog.

 handler.post(new Runnable() {
            @Override
            public void run() {
                progressDialog.setProgress(progress);
            }
        });
Anton Makov
  • 825
  • 2
  • 9
  • 25
0

Thanks to all who helped me, Dear @Anton Makov

private class DownloadReceiver extends ResultReceiver {
    Handler mhandler ;

    public DownloadReceiver(Handler handler) {
        super(handler);
        mhandler = handler;
    }

    @Override
    protected void onReceiveResult(int resultCode, Bundle resultData) {
        super.onReceiveResult(resultCode, resultData);
        if (resultCode == DownloadService.UPDATE_PROGRESS) {
            final int progress = resultData.getInt("progress");
            Log.d("PROGRESS", String.valueOf(progress));
            mhandler.post(new Runnable() {
                @Override
                public void run() {
                    progressDialog.setProgress(progress);
                }
            });
            if (progress == 100) {
                progressDialog.dismiss();
            }

        }
    }
}

This was the final solution

A. El-zahaby
  • 1,130
  • 11
  • 32