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?