ProgressDialog setProgress() 不起作用

ProgressDialog setProgress() doesn't work

我使用以下代码从 android

上的服务器下载文件

下载代码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();
                }
            }
        }
    }
}

代码按预期工作,但 progressDialog.setProgress(progress); 没有显示任何内容

Log.d() 在日志中显示的输出符合预期。

不知道是什么问题?

正如我在第一条评论中提到的,尝试使用工作线程的构造函数(我假设)正在获取的处理程序。你可以访问它,因为你写了 super(handler).

将此代码放在您希望更新进度对话框的位置。

 handler.post(new Runnable() {
            @Override
            public void run() {
                progressDialog.setProgress(progress);
            }
        });

感谢所有帮助过我的人,亲爱的@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();
            }

        }
    }
}

这是最终的解决方案